Guard (computer science)
In computer programming, a guard is a Boolean expression that must evaluate to true if the execution of the program is to continue in the branch in question.[src] Regardless of which programming language is used, a guard clause, guard code, or guard statement is a check of integrity preconditions used to avoid errors during execution.[src] The term guard clause is a software design pattern attributed to Kent Beck who codified many often unnamed coding practices into named software design patterns.[src] The practice of using this technique dates back to at least the early 1960s.[src] The guard clause most commonly is added at the beginning of a procedure and is said to "guard" the rest of the procedure by handling edge cases upfront.[src]
In computer programming, a guard is a Boolean expression that must evaluate to true if the execution of the program is to continue in the branch in question.[src] Regardless of which programming language is used, a guard clause, guard code, or guard statement is a check of integrity preconditions used to avoid errors during execution.[src] The term guard clause is a software design pattern attributed to Kent Beck who codified many often unnamed coding practices into named software design patterns.[src] The practice of using this technique dates back to at least the early 1960s.[src] The guard clause most commonly is added at the beginning of a procedure and is said to "guard" the rest of the procedure by handling edge cases upfront.[src] Other uses include using a Boolean field for idempotence (so subsequent calls are nops), as in the dispose pattern.[src] public String foo(String username) { if (username == null) { throw new IllegalArgumentException("Username is null."); } // Rest of the method code follows here...[src] } from typing import Any, Optional This function has no guard clause def f_noguard(x: Any) -> Optional[int] if isinstance(x, int): # code here return x + 1 else: return None Equivalent function with a guard clause.[src] Note that most of the code is less indented, which makes it easier to read and reason about def f_guard(x: Any) -> Optional[int] if not isinstance(x, int): return None # code here return x + 1 Another example, written in C: // This function has no guard clause int funcNoGuard(int x) { if (x >= 0) { // code here return x + 1; } else { return 0; } } // Equivalent function with a guard clause int funcGuard(int x) { if (x In the following Haskell example, the guards occur between each pair of "|" and "=": f x | x > 0 = 1 | otherwise = 0 This is similar to the respective mathematical notation: f(x) = \left\{ \begin{matrix} 1 & \mbox{if } x>0 \\ 0 & \mbox{otherwise} \end{matrix} \right.[src] In this case the guards are in the "if" and "otherwise" clauses.[src] ==Evolution== A simple conditional expression, already present in CPL in 1963, has a guard on first sub-expression, and another sub-expression to use in case the first one cannot be used.[src] Some common ways to write this: (x>0) -> 1/x; 0 x>0 ?[src] 1/x : 0 If the second sub-expression can be a further simple conditional expression, we can give more alternatives to try before the last fall-through: (x>0) -> 1/x; (x -1/x; 0 In 1966 ISWIM had a form of conditional expression without an obligatory fall-through case, thus separating guard from the concept of choosing either-or.[src] In the case of ISWIM, if none of the alternatives could be used, the value was to be undefined, which was defined to never compute into a value.[src] KRC, a "miniaturized version" of SASL (1976), was one of the first programming languages to use the term "guard".[src] Its function definitions could have several clauses, and the one to apply was chosen based on the guards that followed each clause: fac n = 1, n = 0 = n * fac (n-1), n > 0 Use of guard clauses, and the term "guard clause", dates at least to Smalltalk practice in the 1990s, as codified by Kent Beck.[src] In 1996, Dyalog APL adopted an alternative pure functional style in which the guard is the only control structure.[src] This example, in APL, computes the parity of the input number: parity←{ 2∣⍵ : 'odd' 'even' } ==Pattern guard== In addition to a guard attached to a pattern, pattern guard can refer to the use of pattern matching in the context of a guard.[src] In effect, a match of the pattern is taken to mean pass.[src] This meaning was introduced in a proposal for Haskell by Simon Peyton Jones titled A new view of guards in April 1997 and was used in the implementation of the proposal.[src] The feature provides the ability to use patterns in the guards of a pattern.[src] An example in extended Haskell: clunky env var1 var2 | Just val1 This would read: "Clunky for an environment and two variables, in case the lookups of the variables from the environment produce values, is the sum of the values.[src] ..." As in list comprehensions, the guards are in series, and if any of them fails the branch is not taken.[src] ==References== ==External links== Guard in Free On-Line Dictionary of Computing - FOLDOC, Denis Howe (editor).[src] Guard Clause, WikiWikiWeb The Haskell 98 Report, chapter 3 Expressions.[src] The Mathematica Book, section 2.3.5 Putting Constraints on Patterns The Glorious Glasgow Haskell Compilation System User's Guide, Version 6.4, section 7.3.2.[src] Pattern guards[src]
32 sentences · 32 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Guard (computer science)." encyclopegia.org, 2026, https://encyclopegia.org/wiki/guard-computer-science. Accessed 28 November 2026.
Chicago
"Guard (computer science)." encyclopegia.org. Accessed November 28, 2026. https://encyclopegia.org/wiki/guard-computer-science.
APA
encyclopegia.org. (2026). Guard (computer science). Retrieved November 28, 2026, from https://encyclopegia.org/wiki/guard-computer-science
Accessed November 28, 2026 · CC BY-SA 4.0 · citation page · JSON