Modular Design - Laws and Principles - in OOP and FP

13
Modular Design Laws and Principles in OOP and FP

Transcript of Modular Design - Laws and Principles - in OOP and FP

Page 1: Modular Design - Laws and Principles - in OOP and FP

Modular DesignLaws and Principles

in OOP and FP

Page 2: Modular Design - Laws and Principles - in OOP and FP

Cohesion

Coupling

Page 3: Modular Design - Laws and Principles - in OOP and FP

Cohesion

Coupling

1972

?

Page 4: Modular Design - Laws and Principles - in OOP and FP

● Coincidental Cohesion: unrelated elements

● Temporal Cohesion: operations performed at the same time, e.g. initialization

● Sequential Cohesion: operations form a pipeline - one's output is another's input

● Informational Cohesion: operations are performed on the same data structure

● Functional Cohesion: all elements realize a single, well-defined task, e.g. a function

Cohesion levels

Page 5: Modular Design - Laws and Principles - in OOP and FP

● Content Coupling: a module uses/alters data in another module

● Global-data Coupling: two modules communicate via global data

● Data-structure Coupling:communicate via data structure passed as a parameter (more information than needed)

● Data Coupling: communicate via parameter passing (minimal information)

● No Coupling: independent modules

Coupling levels

Page 6: Modular Design - Laws and Principles - in OOP and FP

A modular unit of code...

● serves a clear purpose

● easy to use

● easy to extend

● easy to replace

● survives outside changes

Single Responsibility

Liskov Substitution

Interface Segregation

Open-Closed Principle

Dependency Inversion

Page 7: Modular Design - Laws and Principles - in OOP and FP

Elements inside a package...

● are used together

● change together

Common Reuse Principle

Common Closure Principle

Page 8: Modular Design - Laws and Principles - in OOP and FP

Packages should...

● be as stable as they are abstract

● depend on more stable packages

● not have cycles

Stable Abstractions Principle

Stable Dependencies Principle

Acyclic Dependencies Principle

Page 9: Modular Design - Laws and Principles - in OOP and FP

The Law of Demeter

"the principle of least knowledge"

A

B

E G

F

DC

B

E

Page 10: Modular Design - Laws and Principles - in OOP and FP

The Demeter Project

Page 11: Modular Design - Laws and Principles - in OOP and FP

Train Wreckclient.getMortgage().paymentCollection().getNextPayment().applyPayment(300.0)

(update-in client [:mortgage :payments] conj (payment 300.0))

wiki.c2.com/?TrainWreck

Page 12: Modular Design - Laws and Principles - in OOP and FP

Tell don’t ask

Feature Envy

Page 13: Modular Design - Laws and Principles - in OOP and FP

in conclusion