09 Software Refactoring Flashcards
what are code smells
- poor structure
2. inappropriate coupling between modules
what is refactoring
- change made to internal structure
- make it easier to understand, cheaper
- behaviour remains the same
refactoring flow
- identify refactor opportunity
- create test cases
- plan refactoring
- apply refactoring
- run tests
- commit
when to refactor
- implementing new functions
- correcting defects
- code reviews
- detecting code smells
- trying to understand how artefact works
types of code smells
- cloning
- complex structures
- variables and parameters
- making changes
- control structures
- design uncertainty
- delegation
- cloning
- duplicated code
- cloned rather than reused
- use extract method
- complex structures
- long method
- contain too many lines of code
- use extract method - large class
- contain too many methods, fields
- too much responsibilities
- use extract class
- variables and parameters
- 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
- making change
- 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
- design uncertainty
- 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
- delegation
- 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
what is self documenting code
excessive use of comments to explain unnecessary complex structures
reduce the need for supplementary documentation
extract method
pros
- more readable code
- less duplication
inline method
pros
- minimise number of unneeded methods
move method
pros
- reduce dependency between class
extract class(split)
pros - single responsibility class are more reliable, tolerant to changes
inline class
pros
- eliminate needless class
encapsulate fields
PRIVATE String name
pros
- bring data closer together
replace data with class
pros
- group primitive data types into its own class
replace magic number with symbolic constant
pros
- reduce duplicate use of number or string
replace method with class
pros
- isolate long method
cons - another class is added which increases complexity
parameterise method
pros
- combine multiple methods by using parameters
remove parameters
pros
- method only contain parameters it truly requires
introduce parameter objects
pros
- replace repeating group of parameter with class
decompose conditional
pros
- decompose conditional codes to clearly named methods
consolidating conditional expressions
pros
- eliminate duplicate control flow codes
replace conditionals with polymorphism
pros
- remove duplicate and almost identical conditions