DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Tomi Adenekan
Tomi Adenekan

Posted on

Klog 1: Cleaning up messes

This is klog, a series of posts on my misfortunes and misadventures in maintaining my project klarg. The naming is a bit unfortunate, but it sounded (and still sounds) good. If you haven't checked it out, what is your purpose in life? why are you here?

This log (or klog 😏) is about my struggles to refactor the code. Those who have refactored code are very familiar with the discomfort of going through messy code and fixing it. The most painful part was realizing that I wrote such evil code. Feelings aside, the problem was that a simple parsing library does not need to contain over 1000 lines of code. It's simply not necessary.

The Problem(s)

1062 lines of code

the problem

I had a set of functions that I needed to call two times, one being for a command line subcommand, and another for a command line command (hmm πŸ€”, that sounds confusing). Anyway, the way I implemented it was that I copied and pasted the function definition from the command to the subcommand, with both the string and number functions, with minor edits. This meant that if any oof happened to my code, I was in deep trouble. I would have to go and change 4 instances of the code, each with different customizations. Not only that, but inside each copy, there were multiple copies. Just think about that (or look at it)

Three copies of the same code

big oof

This is twelve copies that need to change if anything goes wrong.

The Process

I decided that I would make a set of base functions, I would then rebuild on top of them and run the tests to make sure nothing broke. I also decided that I would try to remove as much copying as possible, therefore, I would base (haha) my number collecting function on my string collecting function.

The Result

Somehow, I got exactly what I wanted (maybe it's just me, but what developers envision in their head and what it actually becomes a lot of the times doesn't match up). I reduced the code size 19% (203 lines). I uploaded it to GitHub and made a new release.

Much Reduce

πŸ”₯πŸ‘ŒπŸ”₯

The Oof Factor

While creating a new release, I found out that I had klarg as v0.0.1 instead of v1.0.0.

Conclusion

The end.

Top comments (0)

Build Anything...


Use any Linode offering to create something for the DEV x Linode Hackathon 2022. A variety of prizes are up for grabs, inculding $1,000 USD. πŸ‘€

β†’ Join the Hackathon <-