software design 1: coupling & cohesion

22
Software Design Coupling and Cohesion Attila Magyar Microsec Ltd. 2016

Upload: attila-magyar

Post on 16-Jan-2017

183 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Software Design 1: Coupling & cohesion

Software Design Coupling and Cohesion

Attila MagyarMicrosec Ltd.2016

Page 2: Software Design 1: Coupling & cohesion

Design goals Optimization for maintenance

Cost of change Adding new feature Fixing or modifying existing feature

Understandability How simple is it?

Robustness and correctness How hard is to break it? (code and execution)

Easy to change, easy to understand, hard to break

Page 3: Software Design 1: Coupling & cohesion

Design “principles”, guidelines, metrics SOLID by Michael Feathers and Robert C. Martin

LoD, Tell don’t Ask, Packaging Principles (CCP, CRP), etc 4 rules of simple design by Kent Beck Responsibility Driven Design by Rebecca Wirfs-Brock Test Driven Design (GOOS) by Freeman and Pryce The Physics of Software by Carlo Pescio Connascence by Meilir Page-Jones Cohesion and Coupling by Larry Constantine Code Smells Domain Driven Design by Eric Evans

Page 4: Software Design 1: Coupling & cohesion

Design principles, guidelines, metrics SOLID 4 rules of simple design (DRY) Responsibility Driven Design (Roles) Test Driven Design (GOOS) (Context

independence) The Physics of Software (Entanglement, Friction) Connascence by Meilir Page-Jones Code Smells (FE, Inappr. Intimacy, Message Chain,

Large class, Long Method, Long arg. list) Cohesion and Coupling by Larry Constantine

Page 5: Software Design 1: Coupling & cohesion

Coupling and Cohesion (1979)

Page 6: Software Design 1: Coupling & cohesion

Coupling

A B

Page 7: Software Design 1: Coupling & cohesion

Coupling

Page 8: Software Design 1: Coupling & cohesion

Reasons of coupling

DuplicationDependency

Page 9: Software Design 1: Coupling & cohesion

Types of Coupling Content coupling (high) Common coupling (also known as Global coupling) External coupling Control coupling Stamp coupling (Data-structured coupling) Data coupling Message coupling (low) No coupling

Page 10: Software Design 1: Coupling & cohesion

class A { private B b; [..] public void m() { b.setZ(b.getX() + b.getY()); }}

class B { private int x, y, z;

int getX() { return x; } int getY() { return y; } void setZ(int z) { this.z = z; }}

Page 11: Software Design 1: Coupling & cohesion

class A { private B b; [..] public void m() { b.doYourJob(); }}

class B { private int x, y, z;

void doYourJob() { z = x + y; }}

Page 12: Software Design 1: Coupling & cohesion

Benefits of low coupling

Benefits of low coupling Less ripple effect when changing code Modules are easier to reuse because they are less

context dependent Individual modules are easier to understand

Page 13: Software Design 1: Coupling & cohesion

Coupling between test and code Any assertion upon implementation details

Testing through non-public API Private method/field access

Overly constrain the protocols

Interaction testing does not cause fragility by itself. Don’t blame Mocks.

Page 14: Software Design 1: Coupling & cohesion

Cohesion

Page 15: Software Design 1: Coupling & cohesion

Cohesion

Page 16: Software Design 1: Coupling & cohesion

Types of cohesionCoincidental cohesion (worst)Logical cohesionProcedural cohesionCommunicational/informational

cohesionSequential cohesionFunctional cohesion (best)

Page 17: Software Design 1: Coupling & cohesion

LCOM1, LCOM2 & LCOM3

LCOM1 = Number of pairs of methods that do not share attributes

M1

I3I1 I2

M2 M3 M4

Page 18: Software Design 1: Coupling & cohesion

class A { private F1 f1; private F2 f1; public void m1() { [...] f1.m(); } public void m2() { [...] f2.m(); }}

Page 19: Software Design 1: Coupling & cohesion

class A { private F1 f1; private F2 f1; public void m1() { [...] f1.m(); } public void m2() { [...] f2.m(); }}

Page 20: Software Design 1: Coupling & cohesion

Low cohesion

Unrelated responsibilities/functions imply that the module will have unrelated reasons to change in the future

Replacing parts is difficult

Higher risk of breaking existing functionality when changing

Page 21: Software Design 1: Coupling & cohesion

Summary

Low coupling between modules High cohesion inside modules

Things that change together are packaged together (CCP).

Cohesion = Coupling but with different locality?

Page 22: Software Design 1: Coupling & cohesion

Food for thought Object Design: Roles, Responsibilities, and Collaborations

https://www.amazon.com/Object-Design-Roles-Responsibilities-Collaborations/dp/0201379430 Carlo Pescio — Software Design and the Physics of Software

https://www.youtube.com/watch?v=WPgYju3KnIY Growing Object-Oriented Software, Guided by Tests

https://www.amazon.com/Growing-Object-Oriented-Software-Guided-Tests/dp/0321503627 Jim Weirich - Connascence Examined

https://www.youtube.com/watch?v=HQXVKHoUQxY Understanding the Four Rules of Simple Design

https://leanpub.com/4rulesofsimpledesign Practical Object-Oriented Design in Ruby

https://www.amazon.com/Practical-Object-Oriented-Design-Ruby-Addison-Wesley-ebook/dp/B0096BYG7C

Smalltalk Best Practice Patterns https://www.amazon.com/Smalltalk-Best-Practice-Patterns-Kent/dp/013476904X