Refactor as you code

Mar 13, 2019 by Kolappan N

Refactoring a code refers to the process of altering the code without changing its functionality. Usually, it is done to reduce technical debt or to standardize our code.

Sometimes code refactoring is viewed as a separate task that is done in a scheduled manner. Refactoring should not be a separate task. It should be weaved into your development process. A code that is fresh out of development should not require any refactoring. If a fresh code requires any refactoring then you have got a problem.

A code that is fresh out of development should not require any refactoring.

Let’s see some steps we can take to ensure this. These steps are just some of my best practices that I follow.

1. Use an EditorConfig

There are some things that every programmer in a team does differently. These include small things such as indentation styles, brackets for single line code blocks, etc… Even if your company has a standardized code convention it is difficult to enforce them. Some developers use linting tools to ensure a uniform style during the build pipelines. But I found that using EditorConfig is a much easier way.

EditorConfig contains a config file where you will type the rules for your style guides. In addition to the common style guide, each language has its own addition style rules like this one. One of the reasons I choose EditorConfig over others is that its support is built into Visual Studio. It has a plugin supports for a lot of IDEs and editors.

2. Use some automation

Create some build scripts to automate some refactoring tasks. Let the computer do the heavy lifting. Some of the tasks that can be done at build include removing unused variables, unreachable code, etc…

3. Naming Terminology

Instead of letting each developer choose a name for variables and functions, have a common naming terminology. The terminology itself does not matter much. Just pick one and stick to it.

For example, if you are using GetTeachersList() then use a GetStudentsList() functions. If you are using TeachersListGet() then use a StudentsListGet() functions.

4. Dead Code

If a code is unused or no longer needed delete it. Do you have any old code that is commented out for good, delete it. We have Version Control Systems for a reason. If the old code is needed we can get it from there.

5. Update comment and Docs

Update comments and documentation in your code as you change it. Outdated irrelevant documentation is worse than no documentation. Personally, I update comments or function on relevant codes before each commit.

6. Cite references

If you are using a code or idea from stack overflow or similar site mention it in a comment. I have some programmers who don’t prefer citing their references because they wanted the whole code to appear as theirs. There is no shame in saying that you used a solution found online and saved precious development time. If you have taken a complex regex from Stack Overflow and pasted it in your code then provide a link to it. It will save a great deal of time for anyone who will be working with that code. It helps them understand how the regex is obtained, what does it do, what are its alternatives, etc…

7. Reduce the code complexity while writing

Reduce the complexity of your code while developing it. Have some fixed guidelines and write code accordingly. Some of the guidelines I found helpful are as follows

  1. Fix an upper limit to the number of parameters. If the limit has to be exceeded try to group them into an object
  2. Don’t put the entire function within an if block
  3. Assign one job to a function. Don’t make a function too big
  4. If possible turn complicated conditions into a function which returns boolean.