Bad Code Smells Flashcards

(18 cards)

1
Q

Duplicated Code:

  1. Why is it problematic?
A
  1. Why is it problematic?
    a. ) Duplicated code means that if that code changes, it needs to change in both places making it harder to maintain
    b. ) It adds size and complexity
    c. ) If two or more people write similar code in the same program, this will cause 2 parts of code to work the same job.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Duplicated Code:

  1. How can it be fixed?
A
  1. How can it be fixed?
    a. ) Extract Method - replace duplicate code with a method made from the repeating code. (followed up with pull-up field for the fields used in the method that your pulling)
    b. ) Extract Superclass if that’s impossible, Extract class in one class and use new component in the other
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Feature Envy:

  1. Why is it problematic?
A
  1. Why is it problematic?
    a. ) Feature Envy comes in when an object is accessing properties that do not belong to it.

Basically, if class A is calling lots of methods/ fields from class B more than from itself, maybe the methods calling object B’s methods/fields should belong in object A instead of object B

b.) Dis-organized code

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

Feature Envy:

 2. How can it be fixed?
A
  1. How can it be fixed?
    a. ) Move Field - Move the field to the appropriate class where it is used
    b. ) Move Method - Take the whole method and move it to a different class

OR

c.) Extract Method - Take a piece of code out of a method and in this case place it as its own method in a different class

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

Inappropriate Intimacy:

 1. Why is it problematic?
A
  1. Why is it problematic?
    a. ) High coupling
    b. ) Low cohesion
    c. ) Makes me frown therefore less maintainable

It describes a method that has too much intimate knowledge of another class’s inner workings, inner data, etc. Making the class’ involved less modular, unorganized & less maintainable.

Basically Bi-directional Feature Envy

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

Inappropriate Intimacy:

 2. How can it be fixed?
A
  1. How can it be fixed?
    a. ) Move Field - Move the field to the appropriate class where it is used
    b. ) Move Method - Take the whole method and move it to a different class
    c. ) Extract Class & Hide Delegate
    d. ) If the classes are mutually interdependent, “Change Bi-directional Association to Unidirectional”
    e. ) If this “intimacy” is between a subclass and the superclass then possibly “Replace Delegation with Inheritance”.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Long Method:

1.) Why is it problematic?

A

1.) Why is it problematic?

A method that contains too many lines of code can be confusing for people editing the program.

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

Long Method:

2.) How can it be fixed?

A

2.) How can it be fixed?

If a method contains too many lines of code. More than 10 lines

a. ) Extract Method - Cut the method up into multiple smaller methods that are easier to read and have 1 purpose
b. ) Preserve Whole Object
c. ) Introduce Parameter Object
d. ) Replace Method with Method Object

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

Long Parameter List (e.g. Gallery Step 8, 9):

1.) Why is it problematic?

A
  1. ) Why is it problematic?
    a. ) A long list of parameters for a method can be confusing for people editing the program
    b. ) Can become harder to use/read as the list grows
    c. ) People may enter text in the incorrect order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Long Parameter List (e.g. Gallery Step 8, 9):

2.) How can it be fixed?

A
  1. ) How can it be fixed?
    a. ) Replace Parameter with Method Call

OR

b.) Preserve the Whole Object, to pass the whole object as a parameter

OR

c.) Introduce Parameter Object, if there are several unrelated data elements, sometimes you can merge them into a single parameter object

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

Switch Statements:

1.) Why is it problematic?

A
  1. ) Why is it problematic?
    a. ) Complex switch operator
    b. ) huge sequence of IF statements.
    c. ) If a new condition is added to the environment then you have to find all switch or IF statements and modify it.
    d. ) Decreasing code organization.
    e. ) Hard to maintain

Switch statements are OK in Factory Methods

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

Switch Statements:

2.) How can it be fixed?

A

2.) How can it be fixed?

Isolate and put in the right class via Extract Method then Move Method

After specifying the inheritance structure, Replace Conditional with Polymorphism

If null is one of the options, Introduce Null Object

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

Message Chains:

1.) Why is it problematic?

A
  1. ) Why is it problematic?
    a. ) Dependency between multiple classes via one class calling another then that class calling another. This makes the original message caller dependant on all classes involved.
    b. ) Increases the amount of bloated code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Message Chains:

2.) How can it be fixed?

A
  1. ) How can it be fixed?
    a. ) Hide Delegate
    b. ) Extract Method/Move Method - if it makes sense for the functionality to be at the beginning of the chain instead.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Shotgun Surgery:

1.) Why is it problematic?

A

1.) Why is it problematic?

Making modifications requires that you also make changes to many different classes.

a. ) Disorganized code
b. ) Methods all over the place
c. ) This reduces the efficiency & modularity of classes

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

Shotgun Surgery:

2.) How can it be fixed?

A

2.) How can it be fixed?

a.) Move Method & Move Field -:
Use to move existing class behaviors into a single class. If there is no appropriate class, consider creating a new one.

b.) Inline Class -:
If you find classes after this as almost empty then get rid of redundant classes via Inline Class (combining them with another class).

17
Q

Data Clumps

1.) Why is it problematic?

A
  1. ) Why is it problematic?
    a. ) adds size to a class making it harder to understand
    b. ) Looks disorganized
18
Q

Data Clumps

2.) How can it be fixed?

A
  1. ) How can it be fixed?
    a. ) Extract Class - to move fields to there own class
    b. ) Preserve the Whole Object
    c. ) Introduce Parameter Object