Eliminating Redundancy: Best Practices in Coding Style

Eliminating Redundancy: Best Practices in Coding Style

Introduction

I've observed a tendency among our developers to write code that goes beyond the scope of current functionality. For example:

  async updateEnrollmentStatus(
    id: string,
    status: EnrollmentStatus,
    options?: QueryOptions,
  ) {
    const query = this.model.updateOne(
      {
        id: id,
      },
      { status: status },
    );
    if (options) this.setQueryOptions(query, options);

    const result = await query.exec();
    return result.modifiedCount == 1;
  }

The options parameter is redundant, as they are not used anywhere in the entire codebase. While it's beneficial to maintain flexibility in code for extensibility, it can also inadvertently contribute to technical debt.

Code Redundancy

In the realm of software engineering, redundancy is not inherently detrimental; it acts as an essential safeguard against system failures. Take mission-critical systems like those in aircraft, for example, which are intentionally built with significant redundancy to ensure reliability and prevent downtime. In the context of a codebase, however, the same kind of redundancy can lead to different challenges.

Many developers are inclined to create highly generic functions with the aim of future extensibility. However, from my perspective, this approach may not be advisable. Here’s why.

Polluting the codebase

Effective programming is meant to enhance our efficiency and output. However, inserting superfluous code can obscure the intended functionality and lead to decreased productivity. Moreover, preemptively incorporating features intended for future use often complicates the codebase. This not only makes the debugging process more challenging but can also inflate the cost of maintaining and modifying the code over time. Such preemptive coding assumes future requirements that may evolve differently than anticipated, leading to wasted effort and resources. It is typically more prudent to implement features as they become necessary, ensuring that the code remains relevant, streamlined, and easier to manage.

Solution

Implement TDD (Test Driven Development)

TDD, or Test-Driven Development, is a methodology where unit tests are written before the code itself. Developers then create just enough code to pass these tests, thereby avoiding any unnecessary code that isn't required by the tests. This practice ensures that all new code is immediately covered by tests and helps prevent over-engineering by focusing development efforts solely on what is needed for the desired functionality.

Refactor the code

Code can become obsolete as requirements change over time. This necessitates regular refactoring to remove such outdated segments, ensuring the codebase remains efficient and free of unused code.

Conclusion

Code quality is of utmost importance and should always be at the forefront of a developer's mind. Even though we may occasionally adopt a 'get things done' approach to meet deadlines, we must not compromise on the standards that ensure maintainability, efficiency, and readability of our code.