C# – Working with tuples

Here’s how you create a tuple: Tuples are containers for two or more variables. Without tuples, you’d have to use a class/struct, like this: In other words, tuples provide a convenient alternative to class/structs. Instead of having tons of data container classes, you can use tuples. In this article, I’ll show examples of how to … 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

The bottom-up principles of unit testing, refactoring, and pragmatism

After years of coding and reading dev books I’ve distilled my coding principles down into these three simple principles: unit testing refactoring pragmatism I write these down in my notebook and repeat them to myself on a daily basis. These are my coding principles. It’s important to know your principles. Your principles act as a … Read more