Refactoring

Created

Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure.

  • It keeps the software fit for its purpose and ready for more changes.

Code smells are structures in the code that indicate violation of design principles and negatively impact software quality. Its a surface indication that corresponds to a deeper problem in the system.

Code Smells

Large Class

A class contains many fields/methods/lines of code.

It's less taxing to place a new feature in an existing class than to create a new class. That's why things are normally added, but are rarely taken out.

How do I fix a large class? A bloated class means it has too many responsibility

  • Consider splitting it up into many components (think Single Responsibility Principle)

However, if the issue is not extra responsibility, but more so about handling special cases:

  • Consider extracting subclasses (open/closed principle).
  • Create abstractions (interface) to keep the inheritance hierarchies flat (dependency inversion and interface segregation principle).

Long Method

You should be able to view the whole method on your 5" smartphone screen

When there is a long method, it is a sign that the method has too many responsibilities.

Fix:

  • Break the long method into two or more smaller methods, each should do one precise thing (single responsibility at the method level).
  • The local variables could be so intertwined that it is not so straightforward. In this case, transform the method into separate class so that the local variables become fields of the class. Then, you can split the method into several methods within the same class.

Data Clumps

Data clump is the name given to any group of variables that are passed around together. Occurs where multiple method calls take the same set of parameters.

  • This is an indication that the parameters are related.

Fix:

  • Combine the data clumps together into a class to keep them together.
  • If repeating data comprises the fields of a class, extract those fields into a separate class of their own.

Duplicate Code

Code duplication can cause the same problem to occur in different parts of a system, even when you fix a bug. This is because the bug is not fixed on every occurence. This is why we don't like duplicate code!

Fix In general, move the duplications to a single place.

  • If there is code duplication across two subclasses, move the duplicated code into the parent class
  • If there is code duplication across two separate parts of the code, extract the duplicate code into a stand-alone method.

Side tip: DRY is short for "Don't Repeat Yourself"

Primitive Obssession

Primitive types give little in terms of domain context. If it has a domain meaning, wrap them in a small class to represent the idea.

  • E.g., if you have small objects like currency, email address, zip code, etc. that can be represented with a primitive field, don't!

If you have a large variety of primitive fields, it is possible to logically group some of them in their own class. Or, even better, move the behavior associated with the data into the class too.