Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Latest commit

 

History

History
11 lines (7 loc) · 1020 Bytes

guard.md

File metadata and controls

11 lines (7 loc) · 1020 Bytes

Guard

In computer programming, a guard is a boolean expression that must evaluate to true if the program execution is to continue in the branch in question.

Regardless of which programming language is used, guard clause, guard code, or guard statement, is a check of integrity preconditions used to avoid errors during execution. A typical example is checking that a reference about to be processed is not null, which avoids null-pointer failures. Other uses include using a boolean field for idempotence (so subsequent calls are nops), as in the dispose pattern. The guard provides an early exit from a subroutine, and is a commonly used deviation from structured programming, removing one level of nesting and resulting in flatter code: replacing if guard { ... } with if not guard: return; ....1


[1] Guard (computer science), Wikipedia. (2020). https://en.wikipedia.org/wiki/Guard\_(computer\_science) (accessed September 3, 2020).