Clean Code Flashcards
What are Lehman’s two laws?
A system that is used will be changed.
An evolving system increases its complexity unless work is done to reduce it.
What are the SOLID principles?
Single Responsibility
Open Closed
Liskov Substitution
Interface Segregation
Dependency Inversion
What does the Single Responsibility principle say?
A module should only have a single responsibility
Violation: class Report parses and visualizes report
Solution: split class into Parser and Visualizer
What does the Open Closed Principle say?
Modify behavior by adding new code, not by changing old code
Violation: JPlag-Gen insert strategy hard coded as part of obfuscation method
Solution: use strategy interface, pass implementation
What does the Liskov Substitution principle say?
Subclasses must not change behavior of superclass
Violation: subclass Square of Rectangle, setWidth breaks
Solution: interface Shape
Logically: Precondition must be weaker and postcondition stronger
What does the Interface Segregation Principle say?
Interfaces should be as lean as possible
Violation: broad interface XeroxJob
Solution: Split into PrintJob, StapleJob, …
What does the Dependency Inversion principle say?
Depend upon abstractions, not implementations
Violation: TextEditor depends on Keyboard
Solution: TextEditor depends on Reader, Keyboard implements Reader
What are some other Clean Code principles?
Law of Demeter
Boy Scout Rule
Principle of Least Surprise
Coding Conventions
Don’t Repeat Yourself
Keep it simple, stupid
You ain’t gonna need it
Single level of abstraction