Code Smells: The Unseen Debts in Our Code

Darshit Dhameliya
3 min readJan 2, 2024

--

Hey there, fellow coders! Today, we’re going to take a stroll through the intriguing world of code smells and their sneaky cousin, technical debt. Buckle up, because we’re about to uncover some fascinating mysteries!

Before understanding Code Smells, Let’s first have a look on Technical Debt and How it can gradually increases the Code Smells.

The Story of Technical Debt: The Silent Burden

Imagine you’re on a road trip, and you’ve got a tight schedule to reach your destination. You decide to take a shortcut through a winding, bumpy path. Initially, it saves you time, but as you keep going, you start noticing wear and tear on your car. In coding, when we take shortcuts or write quick and dirty code to meet deadlines, it’s like borrowing time from the future. This debt accrues interest in the form of bugs, maintenance issues, and slowed-down development.

Picture a programmer named John. John is working on a project with a looming deadline. To meet the deadline, they take a shortcut, writing a piece of code quickly without much thought. It gets the job done for now, like taking a loan to cover immediate expenses.

As time goes on, John needs to build upon that code for new features. He keeps adding more code, just like spending more money without paying back the loan. But here’s the catch: that quick code he wrote initially wasn’t optimized or well-structured. It’s like accumulating interest on that borrowed time.

As the project progresses, bugs start popping up unexpectedly. It’s like the interest on that borrowed time is catching up. John realizes that the shortcuts he took are causing issues, just like realizing the interest on a loan has piled up.

The Connection to Code Smells

Now, let’s connect this to code smells. Remember how John rushed through writing that code? Well, that code had some smells — duplicated bits, long, confusing chunks, or maybe it tried to do too much at once.

As John kept building on that shaky foundation, the code smells grew. More duplications cropped up, functions became longer and harder to understand, and the code started becoming tangled, just like a loan accumulating more interest over time.

So, the link between technical debt and code smells is like borrowing time (or taking a loan) when writing code. It’s the promise to pay back that borrowed time later, with interest. But if we keep borrowing without paying off our “coding debts,” those debts accumulate, leading to more code smells and making our code harder to maintain and expand upon.

Now, Let’s understand different types of code smells.

Types of Code Smells

Let’s start with a classic: duplicated code. It’s like having the same thing written in multiple places. Imagine typing out your favorite song lyrics over and over again in different notebooks. That’s a lot of unnecessary work and confusion!

Next up, we’ve got long methods or functions. These are like those super long, run-on sentences that make you lose your breath. Break them down into smaller, more understandable pieces, just like breaking up a long story into chapters.

Next up, we’ve got The Conditional Chaos, also known as “Nested Conditionals” or “Complex Conditional Logic,” occurs when your code’s decision-making structure becomes convoluted with multiple nested if-else statements or switch cases. This complexity often makes the code harder to read, understand, and maintain.

Ever heard of the God Class? It’s a class that tries to do everything. It’s like having a superhero who wants to save the world all by themselves. But hey, even superheroes need a team. Split that class into smaller, more focused ones for a more organized code base.

And then there are those comments that tell a sad story. If your code needs comments to explain what it does, it might be a sign that it’s not clear enough. We want code that speaks for itself, like a book with such a good story, you can’t put it down.

Remember, detecting these smells is like uncovering hidden treasures. Refactoring — that’s the magical tool in your arsenal. It’s like giving your code a spa day — refreshing, rejuvenating, and improving its overall health. We will discuss more about the Refactoring in upcoming blogs.

So, my friends, embrace these code smells as friendly hints nudging you toward cleaner, more elegant code.

--

--