Topic Overview

Welcome to the world of refactoring! In this lesson, we're learning about Code Smells, which are patterns in code that hint at potential problems. Our mission is to help you spot these smells and understand how to improve them or, in programming terms, how to refactor them. We'll delve into the concept of code smells, examine different types, and apply real-world code examples to solidify your understanding. Let's get started!

Introduction to Code Smells

Code smells are signs that something could be amiss in your code. You could compare them to an unpleasant smell in a room. Instead of indicating rotten food or a dirty sock, they signal that your code may not be as readable, efficient, or manageable as it could be.

Consider this bit of code:

The function name calculate is too vague. What exactly does it calculate? For whom? This ambiguity is a sign of a bad naming code smell. Let's see how this and other code smells can be improved!

Duplicate Code

If you notice the same piece of code in more than one place, you may be looking at an example of the Duplicate Code smell. Duplicate code leaves room for errors and bugs. If you need to make a change, you might overlook one instance of duplication.

Here's an example:

This code performs the same operation on different data. Instead of duplicating the operation, we can create a method to handle it:

With this solution, if we need to change the discount or the formula, we can do so in one place: the calculatePrice method.

Too Long Method

A method that does too many things or is too long is harder to read and understand, making it a prime candidate for the Too Long Method smell.

Consider this example:

This function handles too many aspects of order processing, suggesting a Too Long Method smell. A better approach could involve breaking down the functionality into smaller, more focused methods.

For example, the updated code can look like this:

Comment Abuse

Comments within your code should provide useful information, but remember, too much of a good thing can be a problem. Over-commenting can distract from the code itself and, more often than not, it's a sign the code isn't clear enough.

Consider this revised method, which calculates the area of a triangle, now with comments:

While comments explaining the formula might be helpful for some, the code itself is quite straightforward, and the comments on the calculation itself might be seen as unnecessary. If the method's name and parameters are clear, the need for additional comments can be minimized.

Here is how we could change this:

In this version, a single comment placed at the method level provides a high-level overview of what the method does, which is sufficient given the clarity of the method name and parameters. Comments within the method have been removed as they were unnecessary.

Bad Naming

Finally, we have Bad Naming. As the name suggests, this smell occurs when names don't adequately explain what a variable, method, or class does. Good names are crucial for readable, understandable code.

Take a look at the following example:

The names func, $a, and $b don't tell us much about what is happening. A better version could be this:

In this version, each name describes the data or action it represents, making the code easier to read.

Lesson Summary

We've discovered Code Smells and studied common types: Duplicate Code, Too Long Method, Comment Abuse, and Bad Naming. Now you can spot code smells and understand how they can signal a problem in your code.

In the upcoming real-world example-based practice sessions, you'll enhance your debugging skills and improve your code's efficiency, readability, and maintainability. How exciting is that? Let's move ahead!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal