GuardClause.md

A Guard Clause is a Conditional placed at the top of a Routine (and sometime a Loop) which returns early in response to an Novel Scenario. Though it has the same Cyclomatic Complexity as an if then, it's simpler to reason about and eliminates nesting.

function everyOtherValue(myArray) {
    if (myArray == null) return null; // Guard Clause
    
    var result = [];
    for (var c = 0; c < myArray.length; c++) {
        if (c % 2 === 0) continue; // Guard Clause for Loop
        
        result.push(myArray[c]);
    }
}

Guard Clauses are an example of Simplify Early. Guard Clauses are Clean.

#software #complexity