Don’t be afraid to re-write your code

Why you should re-write your code?

In a fast-paced development environment, it happens that we write code that we feel is shitty when we re-visit the same code later.

 

Joey complaining about Ross’s code

 

There could be multiple reasons as to why we write bad code, few of them being –
  • We do not have enough time to think through the design of the feature that we are going to build.
  • Unplanned edge cases hampering with the design we thought would work well.
  • Lack of knowledge about an existing (third party) module that could solve the same problem that we are trying to solve.
  • Sheer ignorance.

Whatever be the reason for writing bad code, but if you get a chance and time to re-write your code, don’t be afraid. Often times, the code that we have written would be huge and confusing even for us. Then, imagine the pain that another developer will through, maintaining your code.

There could be a few reasons as to why we are afraid of re-writing the code. That, it is
  • Tested and working well and we don’t want to mess with the functionality
  • So huge and confusing that it might take a lot of time re-writing and re-testing everything

How to deal with this then?

Breaking the code that we want to re-write helps in mitigating few of the above issues. For example, if you have written code that has many many if-else, you could simply choose to fix that part instead of the whole of your code because
  • It takes less time to replace if-else with better patterns like – object literal or switch or early return
  • It takes less effort in testing these if-else than the whole of your code

 

Or if you have recently read about a pattern that you can easily apply to a part of your old code, you can still do that. By identifying the code you want to re-write without breaking functionality, you can re-write all of your code in time.

 

What you achieve by re-writing your code is,

  • The next time you are using if-else and you use two or more of them, instead of waiting for time to re-write your code, you will fix the code then and there with one of the better alternative patterns of if-else
  • Will think of edge cases more and more as you take up the practice of re-writing code
  • Will learn about new libraries, modules, concepts and patterns that are getting popular in the market which keeps you interview-ready, all the time.
  • Ignorance takes a back seat since you start coding actively

 

Most of all, peace of mind that other developers will not blame you for the legacy that you have left for them to maintain.

 

Happy re-writing!!

 

3 Comments

  1. Totally agree with you. But doesn’t that hit to the productivity of resources if he goes on visiting his code again and again. It’s ok with waterfall model. But when it’s agile it’s really difficult.

    1. This is something that all developers are facing now a days. With the agile model in place, there is little scope for implementing the ideas that we read in our spare time. But then, like we take time out of personal life to read about new things, we should practice those ideas taking some time out of personal life.
      After all, it is for self-improvement and we can always spare time for that.

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons