pnwt.bid
Published on

When should we refactor code?

Authors

Follow the Thai version here.

Overview

Code refactoring is a valuable practice that contributes to the long-term health and sustainability of software projects by improving code quality, maintainability, and developer productivity..

How ?

  • When adding a new feature: When we need to add a new feature and have to deal with existing code, refactoring makes our work easier and cleans up the code.

  • When someone else will work on it next: Refactoring makes it easier for others who will work on the code after us by making it easier to understand and work with.

  • When fixing a bug: Fixing bugs is already difficult, especially if we didn't originally write the problematic code. Refactoring can help reduce future bugs and improve overall code quality.

  • During a code review: During code reviews, reviewers or senior developers may suggest improvements to make the code simpler and easier to read, while maintaining the same functionality.

Conclusion

In reality, when should we do it?

I believe we should refactor whenever we encounter repeated patterns, when we find recurring issues that could be resolved confidently without changing the functionality. This not only makes the code easier to understand but also improves performance. We should do this not just for ourselves but also for those who will work on the code in the future.

Thank you for watching!