Part II - Code Quality Flashcards

1
Q

What is code maintainability?

A

Defined as a degree of effectiveness and efficiency a codebase can be maintained over time

Sub-characteristics:
- modularity
- reusability
- analysability
- modifiability
- testability

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are dimensions of clean code?

A

Readable
Focused (does one thing well)
Efficient (reliable, fast)
Expressive (meaningful names)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is meant by meaningful names?

A

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_____

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is meant by polite code?

A

Abstract the details into functions/methods

Stepdown rule

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are features of well written functions?

A

Small
Do one thing
One level of abstraction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are code smells?

A

Symptom of a problem in the code
Smells do not always indicate problems

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the different types of code smells?

A

Bloaters
Abusers
Couplers
Preventers
Dispensables
Others

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are bloaters?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are abusers?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are couplers?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are dispensables?

A

Lazy class
Data class
Duplicated code -> similar blocks of code (do not repeat yourself!)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are preventers?

A

Code smells that prevent change

Divergent change
Shotgun surgery
Parallel inheritance hierarchies

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is code refactoring?

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is technical debt?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the TDD process?

A

Ensure all tests are passing after code refactoring

Refactor -> RED -> GREEN -> Refactor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are refactoring techniques?

A

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

17
Q

What is code readability?

A

Clearly conveys intent
Easy to understand

18
Q

What makes code more readable?

A

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

19
Q

What causes comments to be a code smell?

A

Add clutter
Misinformation (outdated or incorrect)
Add to maintenance overhead

20
Q

What do good comments do?

A

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

21
Q

What are java code conventions?

A

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)

22
Q

What is a metric and a construct?

A

Construct -> cannot be directly observed or measured

Metric -> can be directly measured

We correlate metrics with a construct

23
Q

What are the uses of LOC?

A

Measure productivity/developer performance

Estimate effort/time/cost

23
Q

What is a size metric for code?

A

Lines of code

24
Q

What is comment density?

A

LOC can be split into non-commented LOC and commented LOC

Comment density = CLOC / LOC

24
Q

What are the limitations of LOC?

A

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

25
Q

What are examples of noise in LOC?

A

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

26
Q

What is a flowgraph?

A

A visual representation of code logic

  • vertices are statements (lines of code that are executed)
  • edges show potential flow between different lines of code
26
Q

What does code complexity mean?

A

More complex code is harder to maintain, reduces productivity

Adds risks (bugs could be added by mistake)

26
Q

What are complexity metrics?

A

Number of Linearly Independent Paths (NLIP)

Halstead’s Measure

Cognitive Complexity

27
Q

What is NLIP?

A

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

27
Q

How do you calculate NLIP?

A

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)

27
Q

What does NLIP tell you?

A

The large the number of paths, the more complex the code

Lower NLIP is better (can be achieved by breaking up large, complex methods)

28
Q

What are limitations?

A

Two pieces of code can have the same NLIP but different amount of complexity

29
Q

What are modularity metrics?

A

Coupling
Cohesion

30
Q

What is modularity?

A

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

31
Q

What is coupling?

A

Considers the number of dependencies between modules
- higher coupling = many dependencies between modules

32
Q

What is cohesion?

A

Considers the number of dependencies within a module
- higher cohesion means elements within a module are related

33
Q
A