Put it down in layman’s terms – code smells series

This post is part of a 10-week series by Dino Esposito (@despos) around a common theme: code smells and code structure.

So far in this series about code smells, we’ve made the point that keeping the language of the code close to the language of the business is crucial to reducing misunderstandings and speeding up development. Naming classes, methods, and sparse values appropriately is part of the job. Today, let’s look at bringing the code even closer to the business language!

In this series: (we will update the table of contents as we progress)

A very common scenario in nearly all types of code is checking the value of a variable against a fixed range of values, mostly constants. In modern programming languages, the enum type is the artifact used to declare a list of named integer constants.

While the value of any constant is an integer, an expression that evaluates to any of the constants is of a type that inherits from System.Enum. In the code snippet, it is RoomState. Enum values are typically assigned to variables or evaluated in conditional statements.

Even when all you need to do is check a value against a constant, you never know what can happen next as the code base grows. Turning explicit constants into enum values is a good first step.

Checking values using the == operator doesn’t help readability, even when names are chosen properly and in full respect of the ubiquitous language of the business domain. Again, ad-hoc methods come to the rescue! We can make our code more explicit by adding a couple of methods.

Here’s a sample Room class with a State property expressed via an enum type.

To programmatically check the state of the room, you can go with plain if statements. However, using ad-hoc methods makes the result much easier to read.

Finding code with structural search

Let’s say that you developed much of your code using plain comparison via the == operator. There might be hundreds of occurrences to find and fix. Is there a quick way to find all of them in a single (and reliable) shot?

Well, ReSharper allows you to do that in a rather comfortable way! You can make use of structural search. From the ReSharper | Find menu, we can Search with Pattern.

ReSharper - Search with pattern

ReSharper’s Search with Pattern looks for all expressions that match the specified code pattern. In this case, you may want to make sure that $var$ (the placeholder for the left-hand side of the comparison) is an expression of type System.Enum or any derived type.

Search for expression with type

Once the pattern is defined, you can run the search and explore results. All that remains to do is to go through each occurrence and apply the necessary fixes – either manually or by using structural replace.

Search results

A wrapper method doesn’t just make your code easier to read, but also hides the details of the comparison being made. Depending on the complexity of the code you are writing, it might not really be far-fetched that at some point the comparison becomes a more sophisticated operation. Now is the time to fix them. The sooner, the better! Your future self will thank you.

Next week, we will look at new vs. factory methods and where it makes sense to use them.

Download ReSharper 2018.1.2 or Rider 2018.1.2 and give them a try. They can help spot and fix common code smells! Check out our code analysis series for more tips and tricks on automatic code inspection with ReSharper and Rider.

This entry was posted in How-To's and tagged , , , , . Bookmark the permalink.

4 Responses to Put it down in layman’s terms – code smells series

  1. Dick Nagtegaal says:

    With this change to the Room class, the RoomState enum doesn’t have to be public anymore. You’d better also make the State property private. In fact, the proposed change allows the Room class to hide its internals.

  2. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #2623

  3. Pingback: Dew Drop - July 17, 2018 (#2767) - Morning Dew

  4. Hi Dino, I love the idea of encapsulating enum conditionals within a class. Certainly makes scanning the code a lot easier.

Leave a Reply

Your email address will not be published. Required fields are marked *