Part II - Code Quality Flashcards
(39 cards)
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
What are refactoring techniques?
Composing methods -> extract method
Moving between objects -> move method, extract class, remove class
Organising data -> replace data value with object
Simplifying conditional expressions -> replace conditional with polymorphism
Simplifying method calls -> introduce parameter object, rename method
Dealing with generalisation -> extra subclass
What is code readability?
Clearly conveys intent
Easy to understand
What makes code more readable?
Clean code is readable (avoid code smells)
Refactoring improves readability
Comments - well made ones that add value make code more readable
**Comments can be code smells if they add no value
What causes comments to be a code smell?
Add clutter
Misinformation (outdated or incorrect)
Add to maintenance overhead
What do good comments do?
Give legal information (copyright notices, license information)
Reveals intent - explain why not what
Clarifications, design rationale, workarounds
Basically - anything that cannot be said by writing better code
What are java code conventions?
Braces are used where optional
Naming conventions
No wildcard imports
One class per file
Use grouping brackets when optional
Declare variables close to where they are used
Dependent methods next to each other (people read from top to bottom)
What is a metric and a construct?
Construct -> cannot be directly observed or measured
Metric -> can be directly measured
We correlate metrics with a construct
What are the uses of LOC?
Measure productivity/developer performance
Estimate effort/time/cost
What is a size metric for code?
Lines of code