Refactor, Refactor, Refactor!
We heard a lot about refactoring, and most of the experts said to refactor your code in any change you get. What does it exactly mean? Does it mean rewriting your code in fewer lines? What is precisely the purpose of doing this?
The idea of refactoring is to evolve your code as you go. Simple things may be to rename variables or method parameters, but others may pass an additional parameter (or drop it), or change its type. The data model may evolve as well.
Refactoring often works hand-in-hand with unit-testing, where the risk of “breaking something” is offset because the automatic testing may likely discover such an issue. In short, the ability to refactor allows one to write more quickly without stressing about some decisions letting the programmer change some of these decisions as time goes by. The added insight is offered by having a workable, if not perfect, solution.
Remember, refactoring is not an excuse to start coding without putting any thought into the design, object model, APIs, etc. However, it lessens the stiffness of some of these decisions. Here are some rules of refactoring that might be a tips for you:
1. When you doing something for the first time just get it done.
2. The second time around, you may be a bit faster, but the code still won’t be as clean as it should be.
3. When you do something for the third time, start refactoring.
As refactoring aims to escalate your code quality, here are some refactoring that you can do; which are always worth paying attention to:
- Functions and variables with unclear names
- An excessive amount of text in one method
- A superfluous amount of comments
- Incorrectly formatted code fragments
During the project, I relied Sonarqube to check my code quality. Through Sonarqube I can see how many duplications in my code and what are the code smells so that I can easily figured out what to refactor.
Here are some advantages we can gain through refactoring.
- Getting rid of duplicated code
- Breaking up a long method into smaller pieces by extracting new methods from sections of the lengthier method
- Breaking up a class that has too many responsibilities into smaller, more targeted classes or subclasses
- Moving methods from a class to another. Often this is done so the methods reside in the same class as the data they operate.
Here’s one of my refactoring during this project:
Previously, some files has their column variable as a table properties. However, there’s several table in the whole app which result to redundancy. Though every table has different columns, but they have some columns in common that led to duplication. So, I create a ColumnGenerator function to generate a customized column for every table.
In full, refactoring is a process that involves revising the code’s source code. It doesn’t introduce new features or changes in the underlying system. It’s a discipline that helps keep the code running smoothly and without introducing bugs. Another benefit of refactoring is that it allows the developers to focus on the details that will drive the implementation of the solution instead of just the code itself.