Bad Code Smells Flashcards
(18 cards)
Duplicated Code:
- Why is it problematic?
- 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.
Duplicated Code:
- How can it be fixed?
- 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
Feature Envy:
- Why is it problematic?
- 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
Feature Envy:
2. How can it be fixed?
- 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
Inappropriate Intimacy:
1. Why is it problematic?
- 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
Inappropriate Intimacy:
2. How can it be fixed?
- 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”.
Long Method:
1.) Why is it problematic?
1.) Why is it problematic?
A method that contains too many lines of code can be confusing for people editing the program.
Long Method:
2.) How can it be fixed?
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
Long Parameter List (e.g. Gallery Step 8, 9):
1.) Why is it problematic?
- ) 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
Long Parameter List (e.g. Gallery Step 8, 9):
2.) How can it be fixed?
- ) 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
Switch Statements:
1.) Why is it problematic?
- ) 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
Switch Statements:
2.) How can it be fixed?
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
Message Chains:
1.) Why is it problematic?
- ) 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
Message Chains:
2.) How can it be fixed?
- ) 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.
Shotgun Surgery:
1.) Why is it problematic?
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
Shotgun Surgery:
2.) How can it be fixed?
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).
Data Clumps
1.) Why is it problematic?
- ) Why is it problematic?
a. ) adds size to a class making it harder to understand
b. ) Looks disorganized
Data Clumps
2.) How can it be fixed?
- ) How can it be fixed?
a. ) Extract Class - to move fields to there own class
b. ) Preserve the Whole Object
c. ) Introduce Parameter Object