HOME

TheInfoList



OR:

In
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, cohesion refers to the ''degree to which the elements inside a module belong together''. In one sense, it is a measure of the strength of relationship between the methods and data of a
class Class, Classes, or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used d ...
and some unifying purpose or concept served by that class. In another sense, it is a measure of the strength of relationship between the class's methods and data. Cohesion is an ordinal type of measurement and is usually described as “high cohesion” or “low cohesion”. Modules with high cohesion tend to be preferable, because high cohesion is associated with several desirable software traits including
robustness Robustness is the property of being strong and healthy in constitution. When it is transposed into a system A system is a group of interacting or interrelated elements that act according to a set of rules to form a unified whole. A system, ...
, reliability,
reusability In computer programming, reusability describes the quality of a software asset that affects its ability to be used in a software system for which it was ''not'' specifically designed. An asset that is easy to reuse and provides utility is conside ...
, and understandability. In contrast, low cohesion is associated with undesirable traits such as being difficult to maintain, test, reuse, or understand. Cohesion is often contrasted with coupling. High cohesion often correlates with
loose coupling In computing and systems design, a loosely coupled system is one # in which components are weakly associated (have breakable relationships) with each other, and thus changes in one component least affect existence or performance of another comp ...
, and vice versa. The
software metric In software engineering and development, a software metric is a standard of measure of a degree to which a software system or process possesses some property. Even if a metric is not a measurement (metrics are functions, while measurements are t ...
s of coupling and cohesion were invented by Larry Constantine in the late 1960s as part of Structured Design, based on characteristics of “good” programming practices that reduced maintenance and modification costs. Structured Design, cohesion and coupling were published in the article ''Stevens, Myers & Constantine'' (1974) and the book ''Yourdon & Constantine'' (1979). The latter two subsequently became standard terms in
software engineering Software engineering is a branch of both computer science and engineering focused on designing, developing, testing, and maintaining Application software, software applications. It involves applying engineering design process, engineering principl ...
.


High cohesion

In
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
, a class is said to have high cohesion if the methods that serve the class are similar in many aspects. In a highly cohesive system, code readability and
reusability In computer programming, reusability describes the quality of a software asset that affects its ability to be used in a software system for which it was ''not'' specifically designed. An asset that is easy to reuse and provides utility is conside ...
is increased, while complexity is kept manageable. Cohesion is increased if: *The functionalities embedded in a class, accessed through its methods, have much in common. *Methods carry out a small number of related activities, by ''avoiding'' coarsely grained or unrelated sets of data. *Related methods are in the same source file or otherwise grouped together; for example, in separate files but in the same sub-directory/folder. Advantages of high cohesion (or "strong cohesion") are: *Reduced module complexity, with fewer operations. *Increased system
maintainability Maintainability is the ease of maintaining or providing maintenance for a functioning product or service. Depending on the field, it can have slightly different meanings. Usage in different fields Engineering In engineering, maintainability ...
, because logical changes in the domain affect fewer modules, and changes in one module require fewer changes in other modules. *Increased module reusability, because application developers will find the
component Component may refer to: In engineering, science, and technology Generic systems *System components, an entity with discrete structure, such as an assembly or software module, within a system considered at a particular level of analysis * Lumped e ...
they need more easily among the cohesive set of operations provided by the module. While in principle a module can have perfect cohesion by only consisting of a single, atomic element – having a single function, for example – in practice complex tasks are not expressible by a single, simple element. Thus a single-element module has an element that is either too complicated to accomplish a task, or too narrow and thus tightly
coupled ''Coupled'' is an American dating game show that aired on Fox from May 17 to August 2, 2016. It was hosted by television personality, Terrence J and created by Mark Burnett, of '' Survivor'', '' The Apprentice'', '' Are You Smarter than a 5th ...
to other modules. Thus cohesion is balanced with both unit complexity and coupling.


Types of cohesion

Cohesion is a qualitative measure, meaning that the source code is examined using a
rubric A rubric is a word or section of text that is traditionally written or printed in red ink for emphasis. The word derives from the Latin , meaning red ochre or red chalk, and originates in medieval illuminated manuscripts from the 13th century or ...
to determine a classification. Cohesion types, from the worst to the best, are as follows: ;Coincidental cohesion (worst): Coincidental cohesion is when parts of a module are grouped arbitrarily. The only relationship between the parts is that they have been grouped together (e.g., a “Utilities” class). Example: :: /* Groups: The function definitions Parts: The terms on each function */ Module A ;Logical cohesion: Logical cohesion is when parts of a module are grouped because they are logically categorized to do the same thing even though they are different by nature (e.g., grouping all mouse and keyboard input handling routines or bundling all models, views, and controllers in separate folders in an MVC pattern). ;Temporal cohesion: Temporal cohesion is when parts of a module are grouped according to the time in which they are processed. The parts are processed at a particular time in program execution (e.g., a function that is called after catching an exception that closes open files, creates an error log, and notifies the user). ;Procedural cohesion: Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g., a function that checks file permissions and then opens the file). ;Communicational/informational cohesion: Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g., a module that operates on the same record of information). ;Sequential cohesion: Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g., a function that reads data from a file and processes the data). ;Functional cohesion (best): Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g.,
Lexical analysis Lexical tokenization is conversion of a text into (semantically or syntactically) meaningful ''lexical tokens'' belonging to categories defined by a "lexer" program. In case of a natural language, those categories include nouns, verbs, adjectives ...
of an XML string). Example: :: /* Groups: The function definitions Parts: The terms on each function */ Module A Module B ;Perfect cohesion (atomic): Example. :: /* Groups: The function definitions Parts: The terms on each function */ Module A Although cohesion is a ranking type of scale, the ranks do not indicate a steady progression of improved cohesion. Studies by Larry Constantine,
Edward Yourdon Edward Nash Yourdon (April 30, 1944 – January 20, 2016) was an American software engineer, computer consultant, author and lecturer, and software engineering methodology pioneer. He was one of the lead developers of the structured analysis tec ...
, and
Steve McConnell Steven C. McConnell is an author of software engineering textbooks such as '' Code Complete'', ''Rapid Development'', and ''Software Estimation''. He is cited as an expert in software engineering and project management. Career McConnell gradua ...
indicate that the first two types of cohesion are inferior, communicational and sequential cohesion are very good, and functional cohesion is superior.


See also

*
Coupling (computer science) A coupling is a device used to connect two shafts together at their ends for the purpose of transmitting power. The primary purpose of couplings is to join two pieces of rotating equipment while permitting some degree of misalignment or end mo ...
*
List of object-oriented programming terms This is a list of terms found in object-oriented programming. A * Abstract class * Accessibility * Abstract method * Abstraction (computer science) *Access control * Access modifiers * Accessor method * Adapter pattern * Aspect-oriented B * B ...
*
Static code analysis In computer science, static program analysis (also known as static analysis or static simulation) is the analysis of computer programs performed without executing them, in contrast with dynamic program analysis, which is performed on programs duri ...


References

{{reflist, refs= {{cite book , author-last=Ingeno , author-first=Joseph , title=Software Architect's Handbook , publisher=
Packt Publishing Packt is a publishing company founded in 2003 and headquartered in Birmingham, UK, with offices in Mumbai, India. Packt primarily publishes print and electronic books and videos relating to information technology, including programming, web ...
, date=2018 , pages=175 , isbn=978-178862406-0
{{Cite journal , doi=10.1147/sj.132.0115 , title=Structured design , journal=
IBM Systems Journal ''IBM Journal of Research and Development'' is a former, peer-reviewed bimonthly scientific journal covering research on information systems. This Journal has ceased production in 2020. According to the ''Journal Citation Reports'' in 2019, the jo ...
, volume=13 , issue=2 , pages=115–139 , date=June 1974 , author-last1=Stevens , author-first1=Wayne P. , author-link1=Wayne Stevens (software engineer) , author-last2=Myers , author-first2=Glenford J. , author-link2=Glenford J. Myers , author-last3=Constantine , author-first3=Larry LeRoy , author-link3=Larry LeRoy Constantine
{{Cite book , title=Structured Design: Fundamentals of a Discipline of Computer Program and Systems Design , author-last1=Yourdon , author-first1=Edward , author-link1=Edward Yourdon , author-last2=Constantine , author-first2=Larry LeRoy , author-link2=Larry LeRoy Constantine , date=1979 , orig-year=1975 , publisher=Yourdon Press , isbn=978-0-13-854471-3 , bibcode=1979sdfd.book.....Y {{cite book , author-last=Marsic , author-first=Ivan , date=2012 , title=Software Engineering , publisher=
Rutgers University Rutgers University ( ), officially Rutgers, The State University of New Jersey, is a Public university, public land-grant research university consisting of three campuses in New Jersey. Chartered in 1766, Rutgers was originally called Queen's C ...
{{cite book , author-first=Steve , author-last=McConnell , author-link=Steve McConnell , title=Code Complete , title-link=Code Complete , edition=2 , date=June 2004 , orig-year=1993 , isbn=978-0-7356-1967-8 , page
168-171
, publisher=Pearson Education


External links


Definitions of Cohesion metrics



Measuring Cohesion in Python
Software architecture Software metrics Programming principles