09 Software Refactoring Flashcards
1
Q
what are code smells
A
- poor structure
- inappropriate coupling between modules
2
Q
what is refactoring
A
- change made to internal structure
- make it easier to understand, cheaper
- behaviour remains the same
3
Q
refactoring flow
A
- identify refactor opportunity
- create test cases
- plan refactoring
- apply refactoring
- run tests
- commit
4
Q
when to refactor
A
- implementing new functions
- correcting defects
- code reviews
- detecting code smells
- trying to understand how artefact works
5
Q
types of code smells
A
- cloning
- complex structures
- variables and parameters
- making changes
- control structures
- design uncertainty
- delegation
6
Q
- cloning
A
- duplicated code
- cloned rather than reused
- use extract method
7
Q
- complex structures
A
- long method
- contain too many lines of code
- use extract method - large class
- contain too many methods, fields
- too much responsibilities
- use extract class
8
Q
- variables and parameters
A
- long parameter list
- too many parameters for a method
- replace parameters with method - feature envy
- method use data of another class extensively more than its own data
- use move method (move method to another class) - data clumps
- different parts of code contain identical group of variables
- use extract class to form new class - primitive obsession
- rely too much on primitive data types
- create object for data types - temporary fields
- variables in method not always needed
- use extract class
9
Q
- making change
A
- divergent change
- when single class is changed, forced to change many other dependent unrelated method
- use extract class to split - shotgun surgery
- making change requires making many small changes to different class
- use move method to a single class - parallel inheritance
- creating a subclass to create subclasses
- use move method to remove hierarchy - switch statement
- too many if statements
- use extract method - refused bequest
- subclass do not need super class methods
- push down methods to subclass and remove hierarchy - alternative classes with interface
- 2 classes perform identical function but with different name
- use rename method
10
Q
- design uncertainty
A
- lazy class
- insufficient independent responsibilities
- use inline class - speculate generality
- unused methods, class, parameters
- for future uses
- comment out - incomplete library class
- libraries stop meeting user requirements but cannot be deleted
- introduce foreign method
11
Q
- delegation
A
- message chain
- one object access data item through a series of calls
- use hide delegation, extract and move method - middle man
- object act as information broker to another object but does not provide any other responsibilities
- use remove middleman, inline method - data class
- class has no method other than getters and setters
- lacks behavioural responsibilities
- use extract method, move method
12
Q
what is self documenting code
A
excessive use of comments to explain unnecessary complex structures
reduce the need for supplementary documentation
13
Q
extract method
A
pros
- more readable code
- less duplication
14
Q
inline method
A
pros
- minimise number of unneeded methods
15
Q
move method
A
pros
- reduce dependency between class