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

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