05. Failure Checks
5.1 Limit single line Failable Expressions count to three
Limit conditional checks/failable expressions on a single line to a maximum of three.
Use the form of
if
with parentheses()
when the number of conditions is less than three.Do:
Don't:
It is essential to avoid splits code over multiple lines
If using more than two words for each expression, a maximum of two expressions on a single line is often more readable.
You can also apply the rule as in a failure context on a single line, don't use more than nine words. When over the limit, use the spaced, multiline form.
Evaluate whether grouping multiple failable conditions into a single
<decides>
function would make the code easier to read and reuse. Note that if the code is only ever used in one place, a "section" comment without an ad hoc function can suffice.Should be rewritten as:
The same guideline applies to expressions in
for
loops. For example:Should be rewritten as:
5.2 Group Dependent Failure Expressions together
When a condition in a failure context depends on a previous failure context succeeding, keep the two conditions together in the same failure context when possible, and follow guideline 5.1.
This improves code locality, which simplifies logical understanding and debugging.
Do
Do
It’s acceptable to split failure contexts if you handle each potential failure (or failure groups) separately.
Was this helpful?