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
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
What is comment density?
LOC can be split into non-commented LOC and commented LOC
Comment density = CLOC / LOC
What are the limitations of LOC?
Have to define what counts as a line of code - blank lines, comments, closing branches, auto-generated code?
Programming language differences - java vs python
Noise in code e.g. duplicate code
based on how you define this estimations of effort time and cost can be incorrect
What are examples of noise in LOC?
C/P - copy and pasted code
Comment
Moved
Language keywords - {,},else
All add to LOC but do not mean more effort, time and cost was required
What is a flowgraph?
A visual representation of code logic
- vertices are statements (lines of code that are executed)
- edges show potential flow between different lines of code
What does code complexity mean?
More complex code is harder to maintain, reduces productivity
Adds risks (bugs could be added by mistake)
What are complexity metrics?
Number of Linearly Independent Paths (NLIP)
Halstead’s Measure
Cognitive Complexity
What is NLIP?
A path starts at enter and follows edges until exit is reached
A set of paths are linearly independent if none of them can be created by combining others in some way
How do you calculate NLIP?
Using flowgraph
e - n + 2
e = number of edges
v = number of vertices
NLIP = d + l
d = number of decision vertices (a cond. statement in the code such as if or while)
What does NLIP tell you?
The large the number of paths, the more complex the code
Lower NLIP is better (can be achieved by breaking up large, complex methods)
What are limitations?
Two pieces of code can have the same NLIP but different amount of complexity
What are modularity metrics?
Coupling
Cohesion
What is modularity?
The concept in which software should be broken in to modules
Information hiding principle:
- modules should expose only stable interfaces
- hide anything that is likely to change
- change in one module should not affect another module
Allows modules to be developed independently and in parallel
What is coupling?
Considers the number of dependencies between modules
- higher coupling = many dependencies between modules
What is cohesion?
Considers the number of dependencies within a module
- higher cohesion means elements within a module are related