DEV Community

Guilherme Ferreira
Guilherme Ferreira

Posted on • Originally published at gsferreira.com on

That flag argument is yelling at you

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.

Top comments (0)