Bad Smells: Data


DTOs are an opportunity. If the data forms a good set, we can usually find a behavior that belongs to the class.

Bad smells resulting from misuse of data structures are:

Primitive Obsession

Symptoms

To do

For Missing Class:

For Simulated Types:

For Simulated Field Accessors:

If the primitive is used to treat certain array elements, apply Replace Array with Object.

Rewards

Contraindications

Data Class

Symptoms

The class consists only of public data, or getters and setters. This makes the client dependent on the mutability and representation of the class.

To do

  1. Apply Encapsulate Field to the block to only allow access to the fields through getters and setters.
  2. Apply Remove Setting Methods to as many methods as you can.
  3. Use Encapsulate Collection to remove direct access to any of the collection type fields.
  4. Look at each client of the object. You will probably find that clients are accessing fields and manipulating results, when the class itself should be doing so.
  5. After analyzing it you will see that you have many similar methods in the class. Then use refactorings such as Rename Method, Extract Method, Add Parameter or Remove Parameter to harmonize signatures and eliminate duplication.
  6. No more accesses to the fields should be needed because the moved methods cover their actual use. Use Hide Method to remove access to getters and setters.

Rewards

Contraindications

Data Clump

Symptoms

To do

Rewards

Contraindications

2019-03-20
Written by Samuel de Vega.
Tags