User Tools

Site Tools


principles:high_cohesion

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
principles:high_cohesion [2013-02-10 23:08] – WIP(relations) christianprinciples:high_cohesion [2019-05-29 15:09] – old revision restored (2016-09-03 22:52) christian
Line 6: Line 6:
 ===== Context ===== ===== Context =====
 /* fill in contexts here: */ /* fill in contexts here: */
-  * [[contexts:Object-Oriented Design]] +  * [[contexts:Object-Oriented Design]] 
 +  * [[contexts:API Design]] 
 +  * [[contexts:Architecture]]
  
  
Line 19: Line 21:
  
 Several kinds of cohesion can be distinguished some of which are strong and some of which are loose. So strong forms of coupling should be preferred: FIXME add explanation of cohesion types Several kinds of cohesion can be distinguished some of which are strong and some of which are loose. So strong forms of coupling should be preferred: FIXME add explanation of cohesion types
 +
 ===== Rationale ===== ===== Rationale =====
  
Line 25: Line 28:
  
 ===== Strategies ===== ===== Strategies =====
 +
 +  * Divide one large module into several smaller but more cohesive ones
 +
 +===== Caveats =====
 +
 +See section [[#contrary principles]].
  
  
 ===== Origin ===== ===== Origin =====
-/* the *primary* source */ 
  
 +W. P. Stevens,  G. J. Myers, L. L. Constantine: //Structured design//
  
 ===== Evidence ===== ===== Evidence =====
Line 35: Line 44:
 /*[[wiki:Proposed]]*/ /*[[wiki:Proposed]]*/
  
-  * [[wiki:Examined]] +  * [[wiki:Examined]]: There are metrics that try to measure cohesion and there are studies relating these cohesion measures to the number of errors found during testing (({{page>resources:A Handbook Of Software And Systems Engineering#reference}})). This correlation is evident. The limitation of these studies is that these cohesion metrics cannot represent the cohesion notion completely. 
-  * [[wiki:Accepted]]+  * [[wiki:Accepted]] The concept of high cohesion is widely known and described in several well-known books for example in {{page>resources:Applying UML And Patterns#reference}}.
  
 /*[[wiki:Questioned]]*/ /*[[wiki:Questioned]]*/
Line 47: Line 56:
 ==== Specializations ==== ==== Specializations ====
  
-  * [[Tell don't Ask/Information Expert]] (TdA/IE): Adhering to information expert means that a module only has responsibilities which belong together. So this increases cohesion. 
   * [[Constantine's Law]]: Constantine's Law is just the combination of HC and LC.   * [[Constantine's Law]]: Constantine's Law is just the combination of HC and LC.
   * [[Single Responsibility Principle]] (SRP): SRP is a stronger version of HC.   * [[Single Responsibility Principle]] (SRP): SRP is a stronger version of HC.
Line 56: Line 64:
   * [[More Is More Complex]] (MIMC): Making a module highly cohesive often results in additional modules. Sometimes it is simpler to assign a minor unrelated responsibility to a module, which lowers the cohesion.   * [[More Is More Complex]] (MIMC): Making a module highly cohesive often results in additional modules. Sometimes it is simpler to assign a minor unrelated responsibility to a module, which lowers the cohesion.
   * [[Model Principle]] (MP): Adhering to HC sometimes means to split up a class into several smaller ones which might correspond to the model less well.   * [[Model Principle]] (MP): Adhering to HC sometimes means to split up a class into several smaller ones which might correspond to the model less well.
 +  * [[Low Coupling]] (LC): A system consisting of one single module has a very low coupling as there are no dependencies on other modules. But such a system also has low cohesion. The other extreme, very many highly cohesive modules, naturally has a higher coupling between the modules. So here a compromise has to be found.
 +
  
 ==== Complementary Principles ==== ==== Complementary Principles ====
  
-  * [[Low Coupling]] (LC): A system consisting of one single module has a very low coupling as there are no dependencies on other modules. But such a system also has low cohesion. The other extreme, very many highly cohesive modules, naturally has a higher coupling between the modules. So here a compromise has to be found.+  * [[Tell don't Ask/Information Expert]] (TdA/IE): IE may help finding solutions with high cohesion. On the other hand it may also be disadvantageous in some cases (see [[Tell don't Ask/Information Expert#caveats]])
   * [[Encapsulate the Concept that Varies]] (ECV): Adhering to HC often results in modules to be split up into several more cohesive ones. ECV gives further advice on how to do that.   * [[Encapsulate the Concept that Varies]] (ECV): Adhering to HC often results in modules to be split up into several more cohesive ones. ECV gives further advice on how to do that.
  
Line 68: Line 78:
  
  
-===== Example =====+===== Examples =====
  
  
Line 76: Line 86:
 /*[[wiki:Incomplete]]*/ /*[[wiki:Incomplete]]*/
 /*[[wiki:Complete]]*/ /*[[wiki:Complete]]*/
 +
 +==== Class level cohesion ====
 +
 +Robert Cecil Martin (Uncle Bob) describes maximal cohesion at class level as "a class in which each variable is used by each method".
 +
 +"In general the more variables a method manipulates the more cohesive that method is to its class." 
 +(Clean Code: A Handbook of Agile Software Craftsmanship - Chapter 10)
  
 ===== Further Reading ===== ===== Further Reading =====
  
-  * Albert Endres and Dieter Rombach: //A Handbook of Software and Systems Engineering//. p. 43pp.+  * Albert Endres and Dieter Rombach: //[[resources:A Handbook of Software and Systems Engineering]]//. p. 43pp.
   * [[wp>Cohesion (computer science)]]   * [[wp>Cohesion (computer science)]]
   * [[wiki>CouplingAndCohesion]]   * [[wiki>CouplingAndCohesion]]
 +  * {{page>resources:Applying UML And Patterns#reference}}
 +
 +===== Discussion =====
  
 +Discuss this wiki article and the principle on the corresponding [[talk:principles:High Cohesion|talk page]].
principles/high_cohesion.txt · Last modified: 2021-10-18 21:36 by christian