DEV Community

Cover image for Code Smell | Divergent change
Jesús Mejías Leiva for Product Hackers

Posted on • Updated on • Originally published at blog.susomejias.dev

Code Smell | Divergent change

Hello, today we are back with the code smells refactoring series and in this case we are going to talk about code smell called Divergent Change, this smell can be detected when we observe that at a specific point in our application we perform too many changes, these points can be a certain class, a .js or .ts file that exports certain functions, etc

A small tip to be able to detect it easily is usually when conflicts occur repeatedly in a certain file, it is usually very likely that this file is complying with the code smell of Divergent change, WARNING: Take with caution not always will comply with this rule, you have to review the code before acting

Cause

  • The module have many responsibilities
  • The context of changes is not clear from day one but new changes are still being implemented in the application
class Hero {
  stamina: number;
  health: number;
  armorHealth: number;
  armorStatus: number;
  armorRarity: string;

  defense(): void {
   // ...
  }

  attack(): void {
   // ...
  }
}
Enter fullscreen mode Exit fullscreen mode

Solution

Split the class using Extract Class:

class Hero {
  stamina: number;
  health: number;
  armor: Armor;

  attack(): void {
   // ...
  }

  defense(): void {
   // ...
  }
}

class Armor {
  health: number;
  status: number;
  rarity: string;

  getHealth(): number {
    // ...
  }
}
Enter fullscreen mode Exit fullscreen mode

Benefits


Thanks for reading me 😊

thanks

Top comments (0)