Clean Code – Conditionals

coryWelcome to Part 4 of this review of the Pluralsight course Clean Code: Writing Code for Humans by Cory House.

Cory is a Microsoft MVP in C#, founder of, avid tech reader, and speaker. He believes in clean code, pragmatic development, and responsive native UIs.

Here are the links to each part in this series:

Part 1 – Writing Code For Humans

Part 2 – Clean Code Principles

Part 3 – Clean Code Naming

Part 4 – Clean Code Conditionals

Part 5 – Clean Code Functions

Part 6 – Clean Code Classes

Part 7 – Clean Code Comments

Part 8 – Clean Code Summary

In this episode, we reflect on our conditional logic.


Boolean Comparisons

“Which would you say in real life? If logged in equals true? Or if logged in?”

Boolean Assignments


I don’t know about you, but this clip is making me hungry.

It’s also a reminder on how you can simplify your boolean assignments and increase your signal to noise ratio.

This is another good example of the power of reading out code in English. The redundancy jumps out at you, prompting you to eliminate it.

Positive Conditionals

Don’t be anti-negative! Just the name “anti-negative” sounds strange – why not just say “positive”? Why not write conditionals positive?

Ternary Elegance

The ternary operator is another opportunity to increase our signal to noise ratio.

Cory discusses the balance you should strike with the use of the ternary operator: not too much and not too little.

Stringly Typed

I love this name “Stringly” Typed. Sounds like Strongly Typed, but then wait, it’s NOT Strongly Typed.

Strongly Typed beats Stringly Typed, Cory gives four very good reasons why it is better to use an Enum.

Magic Numbers

Avoid magic numbers because they make the intent of your code much harder to understand.

Complex Conditionals

Another code smell here. Cory describes some techniques for refactoring the smell away:

  • Intermediate Variables
  • Encapsulate complex conditionals in a well named method

Although it is usually better to write less rather than more code, the example that Cory gives for encapsulating complex conditionals shows that there are times when it is necessary to write some additional code to clarify intent.

Polymorphism vs Enums

Here we see a dirty switch statement and a cleaner solution using the Template Method pattern.

Be Declarative

We see some of the benefits of learning LINQ here.

Something I either completely missed or forgot the first time I watched this course is Cory lists LINQ like implementations available languages other than C#:

  • JavaScript: jLinq
  • Java: LambdaJ (Java 8+ has it’s own implementation of Lamdas so I guess LambdaJ is  obsolete now)
  • Python: Pynq

Table Driven Methods

Here we see an example of dirty code that would be much more suited to existing in a database table.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s