That flag argument is yelling at you

28 May 2020

Sometimes the code yells at you trying to warn you that something is wrong.

Flag arguments are one of those cases. Adding a Boolean argument to condition the execution is an easy fix, but we are doing the wrong thing.

By adding a flag argument to a function, it's clear that we are ignoring the Single Responsibility Principle. The code is yelling at us, trying to explain that. No doubt.

Let's take a look. When a function has a flag argument, we will find something like this (or even worse) in the implementation:

Flag Argument

Wouldn't be better to have something like:

Flag Argument Refactor

When writing code, remember that the goal is to simplify our client's life.


comments powered by Disqus