fbpx

Code Smells

What is a Code Smell?

Code Smells are code pieces that violate programming fundamentals but are not a bug. A Code Smell is a sign that a piece of code has the potential to become complex, unmaintainable, and fragile if it’s not refactored or improved.

Although Code Smells are subjective and based on programming languages and paradigms, you can find some Code Smells across different languages and paradigms.

Type of Code Smells

There are five Code Smell categories.

  • Bloaters
  • Object-Orientation Abusers
  • Change Preventers
  • Dispensables
  • Couplers

Our Course

In this course, each Code Smell category will have its section. The sections will start by describing the category type. Each lesson in the section will talk about a specific code smell; it will have a code example of the Code Smell and a refactored version of the code after implementing a solution from Martin Fowler’s Catalog of Refactorings.

Bloaters

1
Long Functions
5 minutes
2
Large Class
5 minutes
3
Primitive Obsession
5 minutes

We've all seen it and we've all done it. At one point or another, we have an attribute in one of our classes that needs special validation. Whether that's a phone number, a social security number, or even an email. We start off by placing a validation check where we first need it. Soon after, we need to pass it to another object so we need to check it again. Soon it is being used in the Account object, User object, Company object, etc and the validation rule must follow.

This code smell is called Primitive Obsession. A quick google search can turn up plenty of results. Martin Fowler wrote a book on refactoring that does a great job at explaining the problem and the possible solutions.

The problem with Primitive Obsession is that the attribute we're storing isn't just a regular primitive type, it's a primitive type with restrictions. And we want the attribute to comply with the restrictions we apply.

We can solve that by creating a new 'type' or class. This class encapsulates the validation rules and is used as a type when associated with other objects.

4
Long Parameter List
5 minutes
5
Data Clumps
5 minutes

Object-Orientation Abusers

1
Alternative Classes with Different Interfaces
3 minutes
2
Refused Bequest
3 minutes
3
Switch Statements/Repeated Switches
5 minutes
4
Temporary Field
3 minutes

Change Preventers

1
Divergent Change
5 minutes
2
Shotgun Surgery
3 minutes

Dispensables

1
Comments
3 minutes
2
Duplicate Code
2 minutes
3
Data Class
3 minutes
4
Lazy Element
2 minutes
5
Speculative Generality
5 minutes

Couplers

1
Feature Envy
2 minutes
2
Message Chains
3 minutes
3
Middle Man
3 minutes

Be the first to add a review.

Please, login to leave a review
Enrolled: 2 students
Duration: 1.5 hours
Lectures: 19
Level: Intermediate
Price:
Free
This website uses cookies and asks your personal data to enhance your browsing experience.