Why you should re-write your code?
- 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.
- 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?
- 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
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