HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, cohesion refers to the ''degree to which the elements inside a
module Module, modular and modularity may refer to the concept of modularity. They may also refer to: Computing and engineering * Modular design, the engineering discipline of designing complex devices using separately designed sub-components * Modul ...
belong together''. In one sense, it is a measure of the strength of relationship between the
methods Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
and data of a
class Class 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 differently ...
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 themselves. 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 traits of software including
robustness Robustness is the property of being strong and healthy in constitution. When it is transposed into a system, it refers to the ability of tolerating perturbations that might affect the system’s functional body. In the same line ''robustness'' ca ...
, reliability,
reusability In computer science and software engineering, reusability is the use of existing ''assets'' in some form within the software product development process; these ''assets'' are products and by-products of the software development life cycle and in ...
, and understandability. In contrast, low cohesion is associated with undesirable traits such as being difficult to maintain, test, reuse, or even understand. Cohesion is often contrasted with
coupling 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 mov ...
, a different concept. 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 ...
s of coupling and cohesion were invented by
Larry Constantine Larry LeRoy Constantine (born 1943) is an American software engineer, professor in the Center for Exact Sciences and Engineering at the University of Madeira Portugal, and considered one of the pioneers of computing. He has contributed numerous ...
in the late 1960s as part of
Structured Design In software engineering, structured analysis (SA) and structured design (SD) are methods for analyzing business requirements and developing specifications for converting practices into computer programs, hardware configurations, and related manual ...
, 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 systematic engineering approach to software development. A software engineer is a person who applies the principles of software engineering to design, develop, maintain, test, and evaluate computer software. The term '' ...
.


High cohesion

In
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
, if the methods that serve a class tend to be similar in many aspects, then the class is said to have high cohesion. In a highly cohesive system, code readability and
reusability In computer science and software engineering, reusability is the use of existing ''assets'' in some form within the software product development process; these ''assets'' are products and by-products of the software development life cycle and in ...
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 (they are simpler, having fewer operations). *Increased system
maintainability In engineering, maintainability is the ease with which a product can be maintained to: * correct defects or their cause, * Repair or replace faulty or worn-out components without having to replace still working parts, * prevent unexpected working ...
, because logical changes in the domain affect fewer modules, and because changes in one module require fewer changes in other modules. *Increased module reusability, because application developers will find the
component Circuit Component may refer to: •Are devices that perform functions when they are connected in a circuit.   In engineering, science, and technology Generic systems * System components, an entity with discrete structure, such as an assem ...
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 either is too complicated, in order to accomplish a task, or is 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 G ...
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 to be measured 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 la, rubrica, meaning red ochre or red chalk, and originates in Medieval illuminated manuscripts from the 13th ...
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 by when they are processed - the parts are processed at a particular time in program execution (e.g., a function which is called after catching an exception which 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 which 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 which 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 which 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 In computer science, lexical analysis, lexing or tokenization is the process of converting a sequence of characters (such as in a computer program or web page) into a sequence of ''lexical tokens'' ( strings with an assigned and thus identified ...
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 various people including
Larry Constantine Larry LeRoy Constantine (born 1943) is an American software engineer, professor in the Center for Exact Sciences and Engineering at the University of Madeira Portugal, and considered one of the pioneers of computing. He has contributed numerous ...
,
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 tech ...
, 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 gradu ...
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) In software engineering, coupling is the degree of interdependence between software modules; a measure of how closely connected two routines or modules are; the strength of the relationships between modules. Coupling is usually contrasted with ...
*
List of object-oriented programming terms This is a list of terms found in object-oriented programming. Some are related to object-oriented programming and some are not. A *Abstract class *Accessibility * Abstract method *Abstraction (computer science) *Access control *Access modifier ...
*
Static code analysis In computer science, static program analysis (or static analysis) is the analysis of computer programs performed without executing them, in contrast with dynamic program analysis, which is performed on programs during their execution. The term ...


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 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 desig ...
, date=2018 , pages=175 , isbn=978-178862406-0
{{Cite journal , doi=10.1147/sj.132.0115 , title=Structured design , journal= IBM Systems Journal , 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 (; RU), officially Rutgers, The State University of New Jersey, is a public land-grant research university consisting of four campuses in New Jersey. Chartered in 1766, Rutgers was originally called Queen's College, and was ...
{{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


External links


Definitions of Cohesion metrics



Measuring Cohesion in Python
Software architecture Software metrics Programming principles