Skip to content

Remove repetitive code at Joint inherited classes with fucntion extraction #301

@diogomene

Description

@diogomene

While scanning the source code with Qodana, an issue was identified:

In 9 out of the 11 classes extending the Joint abstract class, the following instructions are repeated within the initVelocityConstraints function:

this.m_localCenterA = this.m_bodyA.m_sweep.localCenter;  
this.m_localCenterB = this.m_bodyB.m_sweep.localCenter;  
this.m_invMassA = this.m_bodyA.m_invMass;  
this.m_invMassB = this.m_bodyB.m_invMass;  
this.m_invIA = this.m_bodyA.m_invI;  
this.m_invIB = this.m_bodyB.m_invI;  

This repetition suggests a potential flaw in the method abstraction. There is an opportunity to refactor and extract these repetitive instructions into a reusable module or method. Such a change would significantly reduce code duplication, enhance maintainability, and establish a more robust structure for future growth, where similar repetitions could arise.

Two possible solutions are suggested:

  1. Introduce an additional parent class in the abstraction hierarchy (e.g., Mass2VJoint), inheriting from Joint. This class would encapsulate the necessary attributes (e.g., mass, inertia, and constraints between two bodies) and provide a method to initialize the required constraints.
  2. Add an interface that defines the required attributes, implement it in classes with the duplicated routine, and extract the initialization logic into the Joint class as a method. This method would accept objects extending Joint and implementing the interface.

Either approach would enhance the abstraction layer, simplify the code, and support future scalability.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions