Refactoring & Code Smells Flashcards
Define the term refactoring?
The main purpose of refactoring is to fight technical debt. It transforms a mess into clean code and simple design.
When should you refactor your code?
When adding a feature, when fixing a bug or during a code review.
When should you NOT refactor your code?
If the code need a complete rewrite or there is not enough time to refactor
Define the term code smell?
A code smell is a surface indication that corresponds to deeper problem in the system.
What are some code smells?
Bloaters
Object-Orientation Abusers
Change Preventers
Dispensables
Couplers
What is a data clump? How should you refactor your code if you find one?
It’s a name given to any group of variables which are passed around together throughout various parts of the program.
You should move the code into their own class or passing the entire data object to the method of individual fields.
What is feature envy and what should you do about it?
A method access the data of another object more than its own data.
Move the method or a part of the method to another place or split the method apart to placed in different classes
If a class has several methods which contain conditionals(if/else or switch) blocks that a “type” attribute, what should you do about it?
Create subclasses matching the branches of the conditional. In them, create a shared method and move code from the corresponding branch of the conditional to it. Then replace the conditional with the relevant method call. (Polymorphism)