Code Refactoring: Clean and Readable Code

Patrik Palencar
4 min readDec 17, 2023

--

Introduction

Today, there are loads of people trying to get into the field of information technology. They are trying to learn how to code in multiple ways and especially, as fast as possible. However, these individuals often rush through the long process of getting coding experience, without knowing one of the most fundamental concepts: how to write clean code.

Imagine getting a code from your intern colleague, that you need to work with, but the code is messy and has several “code smells” (reasons for unreadable code). That’s exactly what this blog is about! My goal is to highlight the importance of mastering the composition of a self-explanatory code for every software engineer. This can be achieved by being familiar with the concept of refactoring. I’ll discuss its usefulness and showcase a refactoring example in practice.

Refactoring

When having a code with many code smells: long methods, wrong class names, dead code, etc. Our focus should be on making this code clean. The most convenient way to do this would be to use refactoring. To refactor, means to easily transform messy code into clean and readable one that is also self-explanatory. Several standardized methods exist for refactoring a code.

When do we refactor a code?

Firstly, we could use refactoring, when adding new features to code, that was written by somebody else. By doing this, we are making the code cleaner, but more importantly, we will understand the code. Furthermore, we can use refactoring, when fixing the bugs. Just like in real life, when there’s a mess on a table, it can be quite hard to find a “bug”. But when we organize and clean the table (refactor in coding), we will find the bug much easier. The same happens when facing bugs in the code. Last but not least, we can refactor our own code, when doing a code review.

How to refactor?

To show an example of refactoring a code in practice, I have found a project of a student from IMC University of Applied Sciences Krems on GitHub. I have chosen this project because we can inspect common code smells of a person, who is still learning to code. I think, that it is a great example to show the refactoring in practice. The project is about data preprocessing. You can find the GitHub repository in this link URL.

Practice example

Firstly, we have to look, if the code that we want to refactor “smells”. I went through the code files in this project and I found a file with several code smells. In the file ImageAugmentation.py (code is doing image processing with OpenCV), we can see that the code is not broken down into functions. This makes the code less readable, non-reusable in the future, hard to maintain, plus the author had to use comments to explain the code. The second code smell that we can see there, are the so-called “magic numbers” in the code (numbers, whose purpose is unclear). These numbers make the code less understandable, because we have no idea, what they mean. Next, I will demonstrate how we can refactor the code to enhance its clarity.

See the code with highlighted code smells below.

File ImageAugmentation.py
ImageAugmentation.py, showing which parts of the code should be broken down into different functions

Refactoring

After we have found several code smells we have to use refactoring to eliminate them. To eliminate the first smell, we can refactor the code by grouping it into functions (creating a facade). We can put the chunks of code into the functions and call them at the end of the file. This makes a nice definition of the code structure. After we have grouped the code into several functions we can erase the second code smell. By this code smell, we can again use refactoring. To make the numbers self-explanatory we will assign them names. Now the code is understandable and it is clear what the numbers mean.

See the refactored code below.

Assigned names to the numbers, and put code to functions
Calling the functions I defined at the end of the file and showing the output

After refactoring, the code is not only more readable, but also more maintainable and understandable. You can see the whole refactored code in my forked GitHub repository here.

Conclusion

In conclusion, knowing how to write clean and self-explanatory code is crucial for every software engineer. This blog highlighted the importance of code refactoring in achieving this goal, using a practical example. Thank you for reading, your comments on this topic are welcomed below.

--

--