Thursday, March 2, 2023

Refactoring

Sometimes it just doesn’t fit. They tried calling it “code smells” but that phrase was unsatisfactory, it just doesn’t quite capture the problem.

You know you want the function to be clean. So you move the messy lines up, down, or shift them over. But that pushes the problem to somewhere else, which sets off Yak Shaving. Each new refactor forces another, it feels endless.

You fix the names to be what you thought you should have called them initially. You fiddle with it to remove any noise and syntactic sugar. Maybe throw a comment here or there.

In the middle, you get scared that you are just going around in circles, but you persist. Too late now.

More shifting, more rearrangement, more fiddling. Some pieces get pushed on the wrong side of an architectural line, you push em back. Some parts get packed into tables, you collapse whole trees and lots of redundant code falls away.

You’re at it for days, weeks even. In between, running the code just shows that nothing works. But still, you persist.

And then almost unexpectant it all just snaps into place. A loud, non-existent thundering crack breaks the silence.

You run the code, it is nearly perfect. You check out the code, it is beautiful. Not only are the smells all gone, but most of the bugs have disappeared too. What’s left is simple and readable. It all makes perfect sense now. It looks so obvious you wonder why you didn’t see it right away. You can finally sleep.

The next day someone glances at the code and says “That’s it? Why did it take so long?”. You sigh and know deep down inside, that good code, the really valuable stuff, doesn’t just erupt from the keyboard. Angst, agony, and pain are all essential parts of what is needed to find it in a sea of really bad stuff. The quality of the code is proportional to the work you put into it. The first draft is just the beginning, not the end.

No comments:

Post a Comment

Thanks for the Feedback!