Don't be afraid to refactorMay 27, 2023 | Development
Hello, hello everybody.
Hope you are doing well today.
Well, today I want to talk a bit about refactoring and a thing I experienced today and also read again from a book called "The Pragmatic Programmer".
A lot of developers have a bit of fear, when it comes to refactoring their, especially other people's code. Often times this fear is absolutely justified when I look at those code bases. Nobody likes to deal with legacy code, especially when it's from an era where self-evident concepts from today like object-oriented programming weren't that present.
Okay. You can't blame anyone, but that piece of code needs some sort of fix. I think nearly every developer has encountered this issue. This is a really "painful" moment since you need to take a lot of time to actually understand what this convoluted mess is trying to tell you. In those moments some refactoring would be really appropriate but that's a lot more painful than just implementing the next feature in that manner again. That's the wrong measure, though. You'll have that pain again, and again, and again and you might even end up unsuccessful in those endeavors. This is the moment where you can rightfully start crying in the corner and contemplate why you even chose this career path.
So, please do the refactoring. It may be painful, but it's worth it.
There are many ways to do this properly and make your life a little bit easier. I mean, there are entire books about this topic. But the two best tips I can give you are the following.
- Try to understand exactly what this piece of code wants to achieve. Maybe you even have an entirely new idea to get to the same goal. Who knows.
- Write up a test suite that fulfills all of the needed requirements. With that, you have a way to verify if your refactor was successful or not.
The great this about it is... In the future, you need to look at this piece of code again, and at this point, you won't feel that much pain compared to before. If you even considered some "basic" design principles, your code might even be built in a way so it's easily interchangeable and ready for it.
Again, there are way too many advantages to that.
So... Don't be afraid to refactor your code. Just get through the pain. You can do it. And I promise you... It's worth it.