DEV Community

Rachit Chawla
Rachit Chawla

Posted on

Refactoring Code: A Journey Towards Clean and Maintainable Design

Introduction

In the world of coding, making our programs easy to understand and work on is really important. It's like organizing your room - it's much easier to find things when everything is in its place. In this story, I want to tell you about how I took a messy Python script and turned it into something clean and easy to work with. I'll explain what I did step by step and share what I learned along the way.

The Starting Point

At first, our Python script, called txtToWeb, worked fine, but it was messy. It was like having all your clothes in a big pile - you can find what you need, but it takes a lot of time and effort. Our script had some problems: it was hard to understand, and it had some repeating parts that we could make simpler.

First Commit : Extract parse_config() function

Our script had a part that dealt with settings like language and styles. I took that part out and put it into its own 'helper' function called parse_config(). Think of it like having a separate box just for your accessories. This way, the main part of our script became much cleaner.

Second Commit : Extract write_html file() function

Our script also had a tricky part where it made HTML web pages. I made a new function, write_html_file(), just for that. It's like having a recipe card for your favorite dish. This made our main script much easier to read and understand.

Third Commit : Splitting the code in smaller parts

The biggest change was breaking our main script into smaller pieces. I split the big script into smaller files, each doing a specific job like handling files, converting text, or making HTML. This made our code like a well-organized library.

What I learned new: Git Rebase

During this journey, I discovered a powerful tool in Git - git rebase. It's like having a magic wand for your version control. I used the git rebase main -i command to merge my three commits into one. It allowed me to clean up my commit history, making it more organized and clear. I also used git commit --amend to edit my commit messages, ensuring they were easy to understand and explained my changes accurately.

Link to rebased and amended commit - cadb93b

Conclusion:

In the end, our script transformed from a messy pile into neatly organized parts. Remember, making code better is a marathon, not a race. As we make these improvements, we become better coders, and our programs become even more awesome.

Top comments (0)