How to shorten multiple condition
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Long chains of && or || conditions reduce readability and increase the chance of logic errors. Most languages provide patterns to shorten them: arrays with .includes() or in, early returns, lookup tables, and bit flags. The goal is not fewer characters but clearer intent — a reader should immediately understand what the condition checks. This article shows practical techniques in JavaScript, Python, Java, and C#.
JavaScript: Array.includes()
.includes() replaces repeated === checks with a single readable expression. .every() and .some() generalize AND/OR over arrays of booleans.
Python: in and all/any
Python's in operator works with tuples, lists, and sets. Use a set for large collections (O(1) lookup). Chained comparisons like 18 <= age <= 65 are unique to Python and very readable.
Java: Set.contains() and EnumSet
Set.of() creates an immutable set for clean membership tests. EnumSet is optimized for enum types and uses bit vectors internally.
C#: Contains and Pattern Matching
C# 9 introduced is ... or and is ... and patterns, which are the most concise syntax for multi-value or range checks.
Early Return / Guard Clauses
Guard clauses flatten nested conditions by returning early for invalid cases. The main logic sits at the top level, making it immediately visible.
Lookup Tables
Lookup tables replace conditional branches with data. They are easier to maintain, extend, and test than long if/else or switch chains.
Common Pitfalls
- Creating arrays inside hot loops:
['a', 'b', 'c'].includes(x)allocates a new array every call. For performance-critical loops, define the array as a constant outside the loop or use aSet. - Losing short-circuit evaluation:
[fn1(), fn2(), fn3()].every(Boolean)evaluates all functions upfront. Unlikefn1() && fn2() && fn3(), there is no short-circuit — all three run even if the first returns false. - Using == instead of === with includes: In JavaScript,
[0].includes(false)returnsfalse(strict equality), which is correct. But manual checks with==would match0 == falseas true..includesuses strict comparison. - Readability over brevity: Compressing conditions into a single clever expression can be harder to understand than a few clear lines. Optimize for the reader, not the character count.
- Forgetting the default case: Lookup tables and switch expressions need a fallback for unexpected values. Always include a default case or nullish coalescing (
?? defaultValue).
Summary
- Replace repeated
===/==checks with.includes()(JS),in(Python),Set.of().contains()(Java), oris ... or(C# 9) - Use
.every()/all()for AND chains and.some()/any()for OR chains - Use guard clauses (early returns) to flatten deeply nested conditions
- Use lookup tables (objects/maps/dicts) to replace long if/else or switch chains
- Prioritize readability — the goal is clearer intent, not fewer characters
Related reading
- How to show the last queries executed on MySQL?
- How to shrink the .git folder
- How to shrink/purge ibdata1 file in MySQL
- How to simulate Android killing my process?
- How to solve ' CUDA out of memory. Tried to allocate xxx MiB' in pytorch?
- How to Solve Assignment Problem With Constraints?
- How to solve nan loss?
- How to solve Tn Tn/2 Tn/4 Tn/8 n

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.