09 Software Refactoring Flashcards

1
Q

what are code smells

A
  1. poor structure

2. inappropriate coupling between modules

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

what is refactoring

A
  1. change made to internal structure
  2. make it easier to understand, cheaper
  3. behaviour remains the same
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

refactoring flow

A
  1. identify refactor opportunity
  2. create test cases
  3. plan refactoring
  4. apply refactoring
  5. run tests
  6. commit
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

when to refactor

A
  1. implementing new functions
  2. correcting defects
  3. code reviews
  4. detecting code smells
  5. trying to understand how artefact works
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

types of code smells

A
  1. cloning
  2. complex structures
  3. variables and parameters
  4. making changes
  5. control structures
  6. design uncertainty
  7. delegation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. cloning
A
  1. duplicated code
    - cloned rather than reused
    - use extract method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. complex structures
A
  1. long method
    - contain too many lines of code
    - use extract method
  2. large class
    - contain too many methods, fields
    - too much responsibilities
    - use extract class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. variables and parameters
A
  1. long parameter list
    - too many parameters for a method
    - replace parameters with method
  2. feature envy
    - method use data of another class extensively more than its own data
    - use move method (move method to another class)
  3. data clumps
    - different parts of code contain identical group of variables
    - use extract class to form new class
  4. primitive obsession
    - rely too much on primitive data types
    - create object for data types
  5. temporary fields
    - variables in method not always needed
    - use extract class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. making change
A
  1. divergent change
    - when single class is changed, forced to change many other dependent unrelated method
    - use extract class to split
  2. shotgun surgery
    - making change requires making many small changes to different class
    - use move method to a single class
  3. parallel inheritance
    - creating a subclass to create subclasses
    - use move method to remove hierarchy
  4. switch statement
    - too many if statements
    - use extract method
  5. refused bequest
    - subclass do not need super class methods
    - push down methods to subclass and remove hierarchy
  6. alternative classes with interface
    - 2 classes perform identical function but with different name
    - use rename method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. design uncertainty
A
  1. lazy class
    - insufficient independent responsibilities
    - use inline class
  2. speculate generality
    - unused methods, class, parameters
    - for future uses
    - comment out
  3. incomplete library class
    - libraries stop meeting user requirements but cannot be deleted
    - introduce foreign method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. delegation
A
  1. message chain
    - one object access data item through a series of calls
    - use hide delegation, extract and move method
  2. middle man
    - object act as information broker to another object but does not provide any other responsibilities
    - use remove middleman, inline method
  3. data class
    - class has no method other than getters and setters
    - lacks behavioural responsibilities
    - use extract method, move method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is self documenting code

A

excessive use of comments to explain unnecessary complex structures

reduce the need for supplementary documentation

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

extract method

A

pros

  • more readable code
  • less duplication
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

inline method

A

pros

- minimise number of unneeded methods

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

move method

A

pros

- reduce dependency between class

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

extract class(split)

A
pros
 - single responsibility class are more reliable, tolerant to changes
17
Q

inline class

A

pros

- eliminate needless class

18
Q

encapsulate fields

A

PRIVATE String name

pros
- bring data closer together

19
Q

replace data with class

A

pros

- group primitive data types into its own class

20
Q

replace magic number with symbolic constant

A

pros

- reduce duplicate use of number or string

21
Q

replace method with class

A

pros
- isolate long method

cons
 - another class is added which increases complexity
22
Q

parameterise method

A

pros

- combine multiple methods by using parameters

23
Q

remove parameters

A

pros

- method only contain parameters it truly requires

24
Q

introduce parameter objects

A

pros

- replace repeating group of parameter with class

25
Q

decompose conditional

A

pros

- decompose conditional codes to clearly named methods

26
Q

consolidating conditional expressions

A

pros

- eliminate duplicate control flow codes

27
Q

replace conditionals with polymorphism

A

pros

- remove duplicate and almost identical conditions