Skip to content

Code Refactoring

FullstackCodingGuy edited this page Feb 14, 2024 · 3 revisions

When to refactor

  • Low readability and maintainability: If the code is difficult to understand or modify due to poor organization, inconsistent naming conventions, or lack of comments, refactoring can help improve its clarity and ease of maintenance.

  • Updating to newer technologies or libraries: When migrating to a new technology stack or upgrading libraries, refactoring can help ensure compatibility and smooth integration.

  • Improving performance: When you identify areas of the code with inefficient algorithms or data structures, refactoring can help optimize the code and enhance its performance.

  • Code smells: If you identify recurring patterns or issues in the code that indicate poor design or implementation, such as duplicated code, large classes, long methods, or excessive use of global variables, refactoring should be considered.

  • Enhancing modularity and reusability: If the code has tightly coupled components or lacks modular design, refactoring can help break it into smaller, more manageable pieces with well-defined interfaces, promoting reusability and easier testing.

  • Preparing for new features or changes: Before implementing new functionality or making significant changes to the existing codebase, refactoring can help create a cleaner, more adaptable foundation.

  • Simplifying the codebase: If the code contains redundant or overly complex logic, refactoring can help streamline the code and make it easier to manage.

  • While refactoring the code, one need to ensure that there is no new bugs/issues introduced, and ensure that proper test coverage is in place.

Techniques

Red-Green-Refactor

This technique is an integral part of test-driven development (TDD), a software development methodology that emphasizes writing tests before writing the actual code. The process consists of three main steps: ‍

  • Red: Write a failing test that exposes the issue or required functionality. The goal is to create a test that will fail because the desired functionality has not been implemented yet. This step ensures that the test is valid and can detect the absence of the intended feature.

  • Green: Write the minimum code necessary to pass the test. In this step, you focus on implementing the functionality needed to make the test pass, without worrying about the quality or maintainability of the code. The primary goal is to make the test pass as quickly as possible.

  • Refactor: Improve the code while keeping the test green (i.e., without breaking the functionality). Once the test passes, you can refactor the code to make it cleaner, more efficient, and maintainable. The test serves as a safety net during the refactoring process, ensuring that the external behavior remains unchanged. ‍ This cycle of red-green-refactor is repeated for each new feature or bug fix, promoting a development process where the code is continuously tested and refactored, resulting in higher-quality and more reliable software.

Best Practices

References

Clone this wiki locally