Guard Clause
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 [[TODO]] as an if
then
, it's simpler to reason about and eliminates nesting.
function everyOtherValue(nums) {
// Guard Clause
if (nums == null) return null;
var result = [];
const nl = nums.length;
for (var c = 0; c < nl; c++) {
// Guard Clause, for loop edition
if (c % 2 === 0) continue;
result.push(nums[c]);
}
}
Guard Clauses are an example of Simplify Early. Guard Clauses are Clean.
#software #complexity