Code Smell | Divergent Change

Code Smell | Divergent Change

February 21, 2022

codequality
refactorit
code-smell

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

class Hero {
    stamina: number
    health: number
    armorHealth: number
    armorStatus: number
    armorRarity: string
 
    defense(): void {
        // ...
    }
 
    attack(): void {
        // ...
    }
}

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 {
        // ...
    }
}

Benefits


Thanks for reading me 😊