🤔 Think you know if-else statements? Think Again! 💡
A story of how NOT to write if-else statements
🎓 One of the first concepts taught in any programming classroom is the pitfalls of using go-to statements, which often result in what’s famously known as spaghetti code 🍝. This is a well-understood problem, but why don’t we talk about if-else statements? 🤷♂️ Much like the notorious go-to, poorly structured if-else statements can lead to tangled, confusing logic 🌪️ — setting the stage for bugs 🐛 and a maintenance nightmare 👻.
if-else statements are usually one of the first things every programming language introduces, and they’re fundamental to programming. But this seemingly easy-to-understand topic often makes the code less readable 📚 and harder to maintain 🔧.
First, let’s talk about cognitive load 🧠 and how it impacts us when developing or reading other developers’ code:
Cognitive load, What?
In software development, cognitive load refers to the amount of mental effort required to understand and work with code. High cognitive load can make it difficult to grasp the overall structure and flow of the program, leading to increased chances of errors and reduced productivity.
So, in short, a piece of code that takes less cognitive load to understand is favorable for code maintaining and extending reasons.
Now let’s map connect the dots between this concept and if-else statements
Despite their simplicity, if-else statements can significantly contribute to cognitive load, especially when they are nested or poorly structured. What starts as a straightforward conditional check can quickly spiral into a complex labyrinth of nested branches, making it hard for anyone to trace the logic 🤯.
if-else pitfulls, Who:
1. Nested if-else:
Deeply nested conditions create what is known as “Arrow Code” 🏹, resembling the shape of an arrowhead. This not only makes the code less readable but also harder to maintain.
Check the example below:
2. Long Blocks of Code:
Lengthy blocks of code within if-else statements can obscure the overall logic and flow of the program 🌊.
3. Repetitive Conditions:
Repeatedly checking similar conditions across multiple if-else blocks can lead to code redundancy and potential errors 🔄.
4-Overuse of If-Else for Enumeration:
Using multiple if-else statements to handle different cases of an enumeration instead of using when expressions.
5-Lack of Scalability:
Inflexibility of if-else structures makes them hard to scale. As the number of conditions grows, the complexity increases exponentially.
in the example above, as more roles are added to the system, the function will grow longer and more complex, making it harder to read and maintain.
6-Ignoring Null Safety:
Not properly handling nullability can lead to null pointer exceptions. Kotlin provides comprehensive null safety features, but they must be used correctly.
7-Misusing if-else for Boolean Expressions:
Using if-else statements where a simple boolean expression would suffice, leading to unnecessary verbosity.
Now that we’ve seen some pitfalls of using if-else conditions, let’s see how can we improve them:
if-else, How to:
1. Early Return Pattern:
Utilize early returns to handle edge cases upfront, reducing nesting and making the primary logic more apparent 🛑.
2. Guard Clauses:
These are special cases of early returns, used specifically to handle invalid or exceptional conditions at the start of a function or a block of code 🛡️.
3. Flattening with Kotlin’s `when`:
In Kotlin, replacing complex if-else chains with `when` expressions can significantly flatten and simplify the code structure 📐.
4. Decomposition:
Breaking down complex conditional logic into smaller, well-named functions not only improves readability but also enhances maintainability 🔍.
5. Boolean Algebra:
Simplify complex logical conditions using boolean algebra. This can reduce the number of nested if-else statements 🧮.
6. Avoid Over-Optimization:
While simplification is key, overdoing it can lead to cryptic one-liners. Strive for a balance between simplicity and readability ⚖️.
Conclusion:
Understanding the intricacies of if-else statements and their impact on cognitive load is crucial for writing clean, maintainable code. By avoiding common pitfalls and employing best practices, developers can significantly enhance the readability and quality of their code 💎.
Can you now notify some improvements we can do by learning these patterns to fix the pitfalls? Let me know in the comments!
Great job on making it to the end of this article! 💪
- Follow my YouTube channel for video tutorials and tips on Android development 📺📺
- If you need help with your Android project, Book a 1:1 or a Pair-Programming meeting with me, Book a time now 🧑💻🧑💻🧑💻
If you like this article please can you do me a little favour and hit the 👏clap button as many times! I really appreciate your kindness x❤️👊