Member-only story

The correct way of writing If conditions in your code?

Bending the Clean Code Principles

Anthony Trad
5 min readDec 4, 2022

Introduction

IF/ELSE conditions are part of any algorithm you write daily. While this seem trivial and easy, most people (including me) some time do not use them correctly. Because they are everywhere, you can easily get to the point where you don’t know what you’re doing anymore.

So how do we write If/Else conditions in a clean and scalable way?

The Problem

The problem is really simple, your code is like a little baby that evolves and grow over time. Just like for your child, you want to make sure it grows in a good and healthy way. In a more concrete scenario, you want the following:

  • Scale your code without touching the existing logic.
  • Keep your code simple and understandable by everyone (especially new joiners).
  • Make your code easier to test.
  • Make your code easier to refactor.

Sadky, if you’re just blinding adding IF/ELSE conditions on your code, then you’re losing on all those points and you will have to compensate with some extra effort from your end to go along with this.

--

--

Anthony Trad
Anthony Trad

Written by Anthony Trad

Senior Software Engineer focused on .NET and the Cloud. Reconsidering major principles and patterns, ideas are my own.

Responses (5)