Skip to content

Refactoring proposal to reduce coupling and improve maintainability #3

@MAGSelPad

Description

@MAGSelPad

Description
After analyzing the current implementation of the Java Space Invaders project, several code smells were identified that negatively affect maintainability, extensibility, and readability of the system. The most relevant issues are related to high coupling, low cohesion, and concentration of logic in the Board class.
The Board class currently manages multiple responsibilities, including game flow, movement logic, collision detection, and object state updates. This results in frequent access to internal data of other classes (Alien, Alien.Bomb, Player, Shot), breaking encapsulation and increasing dependency between components.

Identified Problems
The following code smells were identified:

  • Temporary Field: Local variables used only for collision calculations inside update() increase method complexity.
  • Shotgun Surgery: Small changes in collision or movement rules require modifications across multiple classes.
  • Dead Code: Unused constructors introduce unnecessary noise.
  • Data Class: The Commons interface only contains constants without behavior.
  • Lazy Class: The Shot class contains minimal logic and delegates most behavior to Board.
  • Feature Envy: Board frequently accesses data from Alien, Bomb, and Player, indicating misplaced responsibilities.

Proposed Refactorings

  1. To address these issues, the following refactoring techniques are proposed and applied:
  2. Encapsulate Collection – Control access to the aliens collection.
  3. Hide Delegate – Prevent direct access to Alien.Bomb from Board.
  4. Inline Class – Merge Shot into the responsible class due to minimal functionality.
  5. Remove Dead Code – Eliminate unused constructors.
  6. Rename Method – Improve semantic clarity (act() → move()).
  7. Replace Magic Numbers with Symbolic Constants – Improve readability and ease of change.

Benefits

  • Reduced coupling between classes
  • Improved cohesion and clearer responsibilities
  • Easier maintenance and extensibility
  • Cleaner and more readable codebase
  • Better alignment with object-oriented design principles

These refactorings aim to improve code quality while preserving the original functionality of the game. Feedback and suggestions are welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions