Refactoring the Nested Conditionals code smell

Nested conditionals are a code smell because they make the code harder to understand. They tend to grow more and more complicated over time because developers will keep adding conditions and more levels of nesting. The deeper the nesting, the more time it’ll eventually take to refactor it. Code Smell: Nested conditionals.Definition: A conditional block … Read more

Refactoring the Switch Statement code smell

The Switch Statement code smell refers to using switch statements with a type code to get different behavior or data instead of using subclasses and polymorphism. In general, it looks like this: This switch(typeCode) structure is typically spread throughout many methods. This makes the code difficult to extend, and violates the Open-Closed Principle. This principle … Read more

Refactoring the Primitive Obsession code smell

The Primitive Obsession code smell refers to code that is using primitive types (ex: int, bool) instead of classes and enums. This defeats one of the benefits of object-oriented programming: encapsulation. Code Smell: Primitive Obsession. Definition: Using primitives instead of encapsulating them with a class. Solution: Encapsulate the primitive fields: Move them to a new … Read more

Refactoring the Large Class code smell

The Large Class code smells refers to a class that has too many responsibilities. It’s doing too much. Ideally a class should only have one responsibility (Single Responsibility Principle). Code Smell: Large Class Definition: A class has too many responsibilities. Solution: Identify all of the distinct responsibilities of the Large Class. For each responsibility, extract … Read more

How to refactor code that has no tests using the Golden Master technique

The number one rule when refactoring is to always have tests in place before you start refactoring. Without tests, refactoring becomes a risky endeavor. Code that doesn’t have tests is often not designed to be testable. In order to make the code testable, you have to refactor it. See how this seems like an impossible … Read more

Refactoring the Long Method code smell

In this article I’ll be walking through an example of how to refactor the Long Method code smell. Code Smell: Long Method Definition: A method has too many lines of code, making it hard to understand. Solution: Extract duplicate code into a new method Extract code into smaller methods Long Method code smell example Here’s … Read more