Part II - Code Quality Flashcards
What is code maintainability?
Defined as a degree of effectiveness and efficiency a codebase can be maintained over time
Sub-characteristics:
- modularity
- reusability
- analysability
- modifiability
- testability
What are dimensions of clean code?
Readable
Focused (does one thing well)
Efficient (reliable, fast)
Expressive (meaningful names)
What is meant by meaningful names?
Avoid Disinformation -> e.g. don’t refer to something as a list if it is not a list
Make meaningful differences -> don’t use two terms that mean the same thing
Use pronounceable names
Use searchable names
Avoid encodings -> do not encode the type of the variable into the name e.g. phoneString
Be consistent -> pick one word per concept
Solution Domain Names vs Problem Domain Names -> pick words based on who will be reading your code
Classes/Objects -> Nouns
Methods -> verbs
Booleans -> is_____
What is meant by polite code?
Abstract the details into functions/methods
Stepdown rule
What are features of well written functions?
Small
Do one thing
One level of abstraction
What are code smells?
Symptom of a problem in the code
Smells do not always indicate problems
What are the different types of code smells?
Bloaters
Abusers
Couplers
Preventers
Dispensables
Others
What are bloaters?
Long method
Large class
Long parameter list
Data clumps -> multiple method calls take same set of parameters
Primitive obsession -> use of primitives (e.g. String, int) instead of small objects)
What are abusers?
Code smells that violate object-orientation principles
Alternative classes with different interfaces
Temporary field
Switch statements -> complex switch statements should be avoided
Refused bequest -> subclass does not want or need behaviour from base class (wrong class hierarchy)
What are couplers?
Message changes (e.g. a().b().c().d())
Feature envy -> method uses many attributes of another class
Middle man -> class or method only delegates to another class (adds overhead with no value)
What are dispensables?
Lazy class
Data class
Duplicated code -> similar blocks of code (do not repeat yourself!)
What are preventers?
Code smells that prevent change
Divergent change
Shotgun surgery
Parallel inheritance hierarchies
What is code refactoring?
Changing a software system in such a way that it does not alter external behavior yet improves internal structure
Code refactoring requires a set of well made tests!
What is technical debt?
A design or implementation construct that is expedient in the short term, but sets up a technical context that can make a future change more costly or impossible
What is the TDD process?
Ensure all tests are passing after code refactoring
Refactor -> RED -> GREEN -> Refactor