DEV Community 👩‍💻👨‍💻

Cover image for Code Smell 102 - Arrow Code
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

Code Smell 102 - Arrow Code

Code Smell 102 - Arrow Code

Nested IFs and Elses are very hard to read and test

TL;DR: Avoid nested IFs. Even Better: Avoid ALL IFs

Problems

  • Readability

Solutions

  1. Extract Method

  2. Combine Boolean Conditions

  3. Remove accidental IFs

Context

In procedural code, it is very common to see complex nested ifs. This is more related to scripting than object-oriented programming.

Sample Code

Wrong

if (actualIndex < totalItems)
    {
      if (product[actualIndex].Name.Contains("arrow"))
      {
        do
        {
          if (product[actualIndex].price ==