cakefest 2011 - coupling and cohesion
DESCRIPTION
CakeFest 2011 talk on coupling and cohesion. Code examples available on my github account, https://github.com/dogmatic69/cakefest-2011TRANSCRIPT
![Page 1: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/1.jpg)
by: Carl Suttonhttp://dogmatic69.com
Coupling and Cohesion
![Page 2: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/2.jpg)
![Page 3: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/3.jpg)
Before
After
Cohesion
![Page 4: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/4.jpg)
What is "Cohesion"● Modules / Classes do related jobs● Good: High cohesion● Bad: Low cohesion● Three levels to look at (in Cake apps)
○ The Method (does one thing properly)○ The Class (all methods are similar)○ The Plugin (collectively does *a* single job eg: "cart" plugin does not do mini
blog posts)
![Page 5: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/5.jpg)
What is "Cohesion"
● Easier to understand● More robust● Reliability● Maintainable
● Difficult to understand ● Not DRY
Possible Pros
Possible Cons
![Page 6: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/6.jpg)
Examples of CohesionGood:CakePHP's session class- all methods manage one set of data, the sessions (Communicational)
Bad:CakePHP's basics.php file- a bunch of utilities, related because they are not related (Coincidental)
![Page 7: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/7.jpg)
Levels of "Cohesion"Coincidental cohesion
● Lowest level of cohesion, almost none● Parts of a module are grouped arbitrarily● related because they are not related
Eg: ● Utility classes / files● basics.php
![Page 8: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/8.jpg)
Levels of "Cohesion"Logical cohesionParts of a module are grouped because logically they are categorized to do the same thing, even if they are different by nature.
eg: ● Putting all your model files in APP/models
![Page 9: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/9.jpg)
Levels of "Cohesion"Temporal cohesion
● Grouped by when they are processed● the parts are processed at a particular time in program execution
Eg: ● a function which is called after catching an exception
○ closes open connections○ creates an error log○ notifies the user
Procedural cohesion● parts of a module are grouped because they always follow a certain sequence of execution● Similar to Temporal
![Page 10: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/10.jpg)
Levels of "Cohesion"Communicational cohesion
● Parts of a module are grouped because they operate on the same data.
Eg:● SessionComponent + SessionHelper● Cache engines
Bad Model methods:
![Page 11: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/11.jpg)
Levels of "Cohesion"Sequential cohesion
● parts of a module are grouped because the output from one part is the input to another part like an assembly line
Eg: ● a function which reads data from a file and processes the data.● Model methods like Find -> beforeFind -> _find -> afterFind -> $output
![Page 12: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/12.jpg)
Levels of "Cohesion"Functional cohesion
● The best Type● parts of a module are grouped because they all contribute to a single well-defined task of the
module
Eg:● CakeSocket
○ Does one single job (low level network communication)
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away."
![Page 13: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/13.jpg)
Checking for low cohesion● ClassesThatSeemDifficultToName● Seems like splitting it up would be easy● Tests are (much) bigger than the class/method itself● Tests fail in the same way for different reasons● Code is not DRY
Notes● Rules of premature optimisations apply● No point having hundreds of one line methods or one method classes● Sometimes a bit of low cohesion code is needed and/or better than high cohesion
(Utility classes)
![Page 14: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/14.jpg)
CakePHP 2.0 - higher cohesion● Auth was broken into two parts
○ AuthComponent○ Authorisation Adaptor
■ Digest■ Basic■ Form■ Custom
● basic.php removed random functions● CakeRequest created
○ parts of RequestHandler, Dispacher and Controller○ request and response objects
![Page 15: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/15.jpg)
See code examples
![Page 16: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/16.jpg)
Before
After
![Page 17: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/17.jpg)
What is "Coupling"● Relation between bits of code● Code A requires Code B to work● Similar to dependency (inter dependency)● Changes in Code A ripples changes to Code B● Less is more
○ Good: ([low][loose][weak]) coupling○ Bad: ([high][tight][strong]) coupling
![Page 18: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/18.jpg)
What is "Coupling"
● Dumb code● Easier to:
○ read○ debug○ test○ reuse (DRY)○ maintain
● Cheaper / quicker to extend
● More overhead● More magic● Longer dev time / more expensive initially
Possible Pros
Possible Cons
![Page 19: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/19.jpg)
Examples of CouplingGood (loose):CakePHP's ORM (1.x -> )- use MySQL, MsSQL, PostgreSQL or Custom with the same code
CakePHP's JS Engine (1.3.x -> )- use jQuery, Prototype, Prototype or Custom with the same codeCakePHP's Cache (1.x -> )- File, APC, XCache, Memcache or Custom
CakePHP's Sessions (1.x -> )- use cake (file), PHP, database or cache
Bad (tight):CakePHP's test suite (1.x -> )- hard coded to use Simple Test
![Page 20: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/20.jpg)
Levels of "Coupling"Content coupling (high)
● One module modifies / relies on the internal workings of another module.
eg: ● Router::* changes● In 1.3 Helpers (without the analogue code you were stuck)
○ 2.0 can replace helper instances on the fly with another instance■ $this->Html could be MyCustomHtmlHelper extends HtmlHelper
![Page 21: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/21.jpg)
Levels of "Coupling"Common coupling
● Two modules share the same global data.
Eg:● SessionComponent + SessionHelper
○ moving where setFlash() saved messages requires changes to flash()
![Page 22: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/22.jpg)
Levels of "Coupling"External coupling
● Two or more modules share an externally imposed data format○ communication protocol (imap, pop3, ftp, http etc)○ device interface○ file format (csv, xml, json etc)
Eg:● EmailComponent
○ RFC 2822 - Internet Message Format● Database Driver MySQL, MSSQL etc
○ need to use SQL language
![Page 23: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/23.jpg)
Levels of "Coupling"Control coupling
● one module controlling the flow of another● passing it information on what to do
Eg: ● Controller starting up a Component
![Page 24: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/24.jpg)
Levels of "Coupling"Stamp coupling (Data-structured coupling)
● modules share a composite data structure and use only a part of it
eg: ● function foo($options){ return $options['bar'] * 10;}● validation methods only get the field they are validating (good)
![Page 25: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/25.jpg)
Levels of "Coupling"Data coupling
● modules share data through, for example, parameters. ● Each datum is an elementary piece, and these are the only data shared.
eg: ● validation methods in a Model class
![Page 26: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/26.jpg)
Levels of "Coupling"Message coupling (low)This is the loosest type of coupling. It can be achieved by state decentralization. JS Engine, Cache Engine, ORM etc
No coupling● Modules do not communicate at all with one another. ● Only time there is *no* coupling is when you have a formatted HDD :P
Eg: ● EmailComponent is not coupled directly to Models
![Page 27: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/27.jpg)
Levels of "Coupling"Message coupling (low)
● This is the loosest type of coupling. ● Achieved by state decentralization.
Eg: ● JS Engine, Cache Engine, ORM etc● you call a main 'engine' class that calls the correct class that you need● Dependency injection● Observable
![Page 28: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/28.jpg)
Levels of "Coupling"No coupling
● Modules do not communicate at all with one another. ● rm -rf /* will remove all coupling
Eg: ● EmailComponent is not coupled directly to Models
![Page 29: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/29.jpg)
Checking for high coupling● Changes to code A requires changes to code B, C etc● Reuse of code is difficult / impossible● Testing is super complicated
![Page 30: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/30.jpg)
CakePHP 2.0 - lower coupling● Auth was broken into two parts
○ Authorisation Adaptor - custom authorisation classes● Dispatcher
○ Parts moved to the Controller so its easy to change / extend (not coupled to the one cake way)
● switch helpers and components on the fly○ no need to use $this->MyCustomHtmlHelper->foo()
![Page 31: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/31.jpg)
See code examples
![Page 32: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/32.jpg)
Related stuff
● Coupling● Cohesion● DRY● Dependency● Cyclomatic complexity● Adapter Pattern● Dependency_injection● Observer● SRP
![Page 33: CakeFest 2011 - Coupling and cohesion](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55545f52b4c905aa798b48da/html5/thumbnails/33.jpg)
Links
dogmatic69CakePHPInfinitas