Cleaner Code: Tackling Arrowhead Anti-Pattern

Comviva MFS Engineering Tech Blog
5 min readJan 23, 2024

--

By Swagata Acharyya

Have you ever found yourself dealing with a labyrinth of code, where each if statement leads you deeper into a maze of confusion? This common scenario is the hallmark of the infamous Arrowhead Anti-pattern. Basically this is a coding style notorious for its deeply nested conditional statements that challenge readability and complicate maintenance.

Seen in many codebases, the Arrowhead Anti-pattern is like a silent creeper in software development, often unnoticed until it entangles the code in complexity. Various lint tools, if setup properly can detect this pattern earlier. But this is important to understand why this is such a menace and how it could be avoided.

In this article, we'll dive into the depths of the Arrowhead Anti-pattern, exploring its impact on code quality and maintainability. Our journey will not just highlight the problems but also will try to figure out effective strategies for mitigating this pattern, ensuring our code remains clean, efficient, and, most importantly, comprehensible.

What is ArrowHead Anti-Pattern (Deep Nesting)?

Have you ever seen code that looks a bit like an arrow’s tip? That’s the Arrowhead Anti-pattern for you. Think of it like this: if (this thing) { ... } else if (that thing) { if (another thing) { ... } else { ... } }. Each layer of if and else adds another twist and turn, making your code look like the jagged edge of an arrowhead.

Each time you add a new if, your code takes a step to the right, slowly forming a shape like the pointy end of an arrow. That's why it's called the Arrowhead.

It's not just about the name, though. This pattern is a sign that your code is turning into a tricky maze, hard to follow and fix. The arrowhead shape is like a little warning sign saying, "Hey, it's time to flatten this out and make it easier to understand."

Addressing the ArrowHead

Following methodologies are used in general when it comes to breaking down ArrowHead antipattern.

  • Breaking Down Complex Methods: This is no brainer. If a chunk of code gets too complex, refactor the earlier complex code snippet into smaller, more manageable functions.
  • Using Early Returns and Guard Clauses: Early returns and guard clauses simplify the logic by exiting a function early if certain conditions are met, thus reducing the need for deep nesting. In our example later, we will illustrate this.
  • Keep an eye on Complexity and Execution Time: Keep an eye on the complexity. Post-refactoring, recompute the complexity and execution time to see the improvements.
  • Maintainability Benchmarking: This is important to have some benchmark when it comes to acceptable deep nesting. Code can be assessed for maintainability using metrics like the Cyclomatic Complexity or Maintainability Index. Tools like SonarQube and other lint tools provide an objective measure of the code’s readability and maintainability. A lower Cyclomatic Complexity, for example, typically correlates with easier-to-maintain code, reflecting the benefits of the refactoring process.

Let’s dig in with an example

Let’s start with some code. I will show some example with Kotlin, but the anti pattern is present in every language. If you don’t use Kotlin, feel free to convert it to your preferred language.

fun validateInput(input: String): Boolean {
if (input.isNotEmpty()) {
if (input.length > 5) {
if (input.contains("@")) {
if (input.endsWith(".com")) {
// Assume some complex logic here
println("Input is valid")
return true
} else {
println("Input must end with .com")
}
} else {
println("Input must contain @")
}
} else {
println("Input must be more than 5 characters")
}
} else {
println("Input cannot be empty")
}
return false
}

In this example:

  • The function validateInput checks if the input is not empty, has a length greater than 5, contains an "@" symbol, and ends with ".com".
  • The code is deeply nested with multiple if conditions, forming the "arrowhead" shape.
  • This code certainly needs some time to understand. If a simple logic like this can be inconvenience when it comes to understanding, imagine what can happen with much more complex code and bigger nesting.

Cyclomatic Complexity:

In this example, each if statement adds a new path, and each else adds another. Thus, this function has a Cyclomatic Complexity of 9 (1 for the method start, plus 8 for the eight if/else branches).

Let’s see what all approaches we can take to solve this problem.

With Guard Clauses:

fun validateInput(input: String): Boolean {
if (input.isEmpty()) {
println("Input cannot be empty")
return false
}
if (input.length <= 5) {
println("Input must be more than 5 characters")
return false
}
if (!input.contains("@")) {
println("Input must contain @")
return false
}
if (!input.endsWith(".com")) {
println("Input must end with .com")
return false
}
println("Input is valid")
return true
}

Cyclomatic Complexity: The refactored function has a Cyclomatic Complexity of 5 (1 for the method start, plus 4 for the four if statements).

Analysis

  • The original code with the Arrowhead Anti-pattern has a higher Cyclomatic Complexity, indicating it’s more complex and has more independent paths, making it harder to test and maintain.
  • The refactored code, with guard clauses, significantly reduces the complexity. It’s more straightforward, easier to understand, and has fewer paths to test, indicating better maintainability and readability.

Using Language Specific Construct

We can further reduce the complexity of the refactored code by consolidating the validation checks into a single expression. This approach can reduce the number of explicit paths in the code, thereby lowering the Cyclomatic Complexity.

fun validateInput(input: String): Boolean {
val errorMessage = when {
input.isEmpty() -> "Input cannot be empty"
input.length <= 5 -> "Input must be more than 5 characters"
!input.contains("@") -> "Input must contain @"
!input.endsWith(".com") -> "Input must end with .com"
else -> null
}

errorMessage?.let {
println(it)
return false
}

println("Input is valid")
return true
}

In this version:

  • A when expression is used to consolidate the conditions. This way, the code checks each condition in sequence and assigns an appropriate error message if any condition fails.
  • If any validation fails, the corresponding error message is printed, and the function returns false. If all validations pass, the function prints "Input is valid" and returns true.

Cyclomatic Complexity Analysis:

  • The Cyclomatic Complexity of this version is 2 (1 for the method start, plus 1 for the when expression). This is a further reduction from the earlier versions and represents a more streamlined and maintainable approach.

This approach demonstrates how Kotlin’s powerful features, like the when expression, can be leveraged to write more concise and less complex code, which is easier to understand, maintain, and test.

Conclusion

As we wrap up our exploration of the Arrowhead Anti-pattern, it’s clear that recognizing and addressing this pattern is crucial for maintaining clean, efficient, and manageable code. The journey from a convoluted arrowhead structure to a streamlined and clear codebase not only enhances readability but also significantly eases maintenance efforts. It’s a transformation that can have profound impacts on the overall health of your software.

We should take a proactive approach in our projects. Periodically review the codebase for signs of the Arrowhead Anti-pattern and similar complexities. Setup tools like SonarQube and set up benchmark so the overall code quality is maintained.

The pursuit of clean code is ongoing; it’s about making continuous improvements, one function at a time. By prioritising practices that simplify and clarify our code, we contribute to a codebase that’s not just functional, but also a joy to work with for us and our team.

--

--