*** Welcome to piglix ***

Guard (computer science)


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 code or a guard clause is a check of integrity preconditions used to avoid errors during execution. A typical example is checking that a reference about to be processed be 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. Guard code 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; ....

The term is used with specific meaning in APL, Haskell, Clean, Erlang, occam, Promela, OCaml, Swift and Scala programming languages. In Mathematica, guards are called constraints. Guards are the fundamental concept in Guarded Command Language, a language in formal methods. Guards can be used to augment pattern matching with the possibility to skip a pattern even if the structure matches. Boolean expressions in conditional statements usually also fit this definition of a guard although they are called conditions.


...
Wikipedia

...