Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Guard (computer science)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Concept in computer science}} In [[computer programming]], a '''guard''' is a [[Boolean expression]] that must evaluate to true if the [[Execution (computing)|execution]] of the [[Computer program|program]] is to continue in the branch in question. Regardless of which [[programming language]] is used, a '''guard clause''', '''guard code''', or '''guard statement''' is a check of integrity [[precondition]]s used to avoid errors during execution. 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, the practice of using this technique dates back to at least the early 1960's. 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 edgecases upfront. == Uses == A typical example is checking that a [[Reference (computer science)|reference]] about to be processed is not null, which avoids [[Null pointer|null-pointer]] failures. Other uses include using a Boolean field for [[idempotence]] (so subsequent calls are [[NOP (code)|nop]]s), as in the [[dispose pattern]].<syntaxhighlight lang="java"> public String foo(String username) { if (username == null) { throw new IllegalArgumentException("Username is null."); } // Rest of the method code follows here... } </syntaxhighlight> === Flatter code with less nesting === 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:<ref name="beck">{{cite book |title=Smalltalk Best Practice Patterns, |first=Kent |last=Beck |authorlink=Kent Beck |year=1997 |section=Guard Clause |pages=178β179}}</ref> replacing <code>if guard { ... }</code> with <code>if not guard: return; ...</code>. Using guard clauses can be a [[refactoring]] technique to improve code. In general, less nesting is good, as it simplifies the code and reduces cognitive burden. For example, in [[Python (programming language)|Python]]: <syntaxhighlight lang="python"> # This function has no guard clause def f_noguard(x): if isinstance(x, int): #code #code #code return x + 1 else: return None # Equivalent function with a guard clause. Note that most of the code is less indented, which makes it easier to read and reason about def f_guard(x): if not isinstance(x, int): return None #code #code #code return x + 1 </syntaxhighlight> Another example, written in [[C (programming language)|C]]: <syntaxhighlight lang="c"> // This function has no guard clause int funcNoGuard(int x) { if (x >= 0) { //code //code //code return x + 1; } else { return 0; } } // Equivalent function with a guard clause int funcGuard(int x) { if (x < 0) { return 0; } //code //code //code return x + 1; } </syntaxhighlight> == Terminology == The term is used with specific meaning in [[APL (programming language)|APL]], [[Haskell (programming language)|Haskell]], [[Clean programming language|Clean]], [[Erlang programming language|Erlang]], [[Occam (programming language)|occam]], [[Promela]], [[OCaml]], [[Swift (programming language)|Swift]],<ref>{{Cite web | url = http://nshipster.com/guard-and-defer/ | title = guard & defer | last = Cook | first = Nate | website = NSHipster | access-date = 2016-02-26 }}</ref> [[Python (programming language)|Python]] from version 3.10, and [[Scala (programming language)|Scala]] programming languages.{{Citation needed|date=March 2012}} 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 (programming)|conditional statement]]s usually also fit this definition of a guard although they are called ''conditions''. == Mathematics == In the following Haskell example, the guards occur between each pair of "|" and "=": <syntaxhighlight lang="haskell"> f x | x > 0 = 1 | otherwise = 0 </syntaxhighlight> This is similar to the respective mathematical notation: <math> f(x) = \left\{ \begin{matrix} 1 & \mbox{if } x>0 \\ 0 & \mbox{otherwise} \end{matrix} \right. </math> In this case the guards are in the "if" and "otherwise" clauses. == Multiple guards == If there are several parallel guards, they are normally tried in a top-to-bottom order, and the branch of the first to pass is chosen. Guards in a list of cases are typically parallel. However, in Haskell [[list comprehension]]s the guards are in series, and if any of them fails, the list element is not produced. This would be the same as combining the separate guards with [[logical conjunction|logical AND]], except that there can be other list comprehension clauses among the guards. ==Evolution== A simple conditional expression, already present in [[CPL programming language|CPL]] in 1963, has a guard on first sub-expression, and another sub-expression to use in case the first one cannot be used. Some common ways to write this: (x>0) -> 1/x; 0 x>0 ? 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<0) -> -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. 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. [[Kent Recursive Calculator|KRC]], a "miniaturized version"<ref>{{cite web|last1=Turner|first1=D. A.|title=Some History of Functional Programming Languages|url=https://www.cs.kent.ac.uk/people/staff/dat/tfp12/tfp12.pdf}}</ref> of [[SASL programming language|SASL]] (1976), was one of the first programming languages to use the term "guard". Its function definitions could have several clauses, and the one to apply was chosen based on the guards that followed each clause: <!-- SASL isn't Haskell, of course, but the syntax is close enough... --> <syntaxhighlight lang="haskell"> fac n = 1, n = 0 = n * fac (n-1), n > 0 </syntaxhighlight> <!-- add something on how Guarded commands fit in --> Use of guard clauses, and the term "guard clause", dates at least to [[Smalltalk]] practice in the 1990s, as codified by [[Kent Beck]].<ref name="beck"/> In 1996, Dyalog APL adopted an alternative pure functional style in which the guard is the only control structure.<ref>{{Cite web|url=http://www.dyalog.com/uploads/documents/Papers/dfns.pdf|title=Direct Functions in Dyalog APL|last=Scholes|first=John|date=|website=|publisher=|access-date=}}</ref> This example, in APL, computes the parity of the input number:<syntaxhighlight lang="apl"> parityβ{ 2β£β΅ : 'odd' 'even' } </syntaxhighlight> ==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. In effect, a match of the pattern is taken to mean pass. This meaning was introduced in a proposal for Haskell by [[Simon Peyton Jones]] titled [https://web.archive.org/web/20160203203811/http://research.microsoft.com/en-us/um/people/simonpj/Haskell/guards.html A new view of guards] in April 1997 and was used in the implementation of the proposal. The feature provides the ability to use patterns in the guards of a pattern. An example in extended Haskell: <syntaxhighlight lang="haskell"> clunky env var1 var2 | Just val1 <- lookup env var1 , Just val2 <- lookup env var2 = val1 + val2 -- ...other equations for clunky... </syntaxhighlight> 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. ..." As in [[list comprehension]]s, the guards are in series, and if any of them fails the branch is not taken. == See also == * [[Assertion (computing)|Assertion]] * [[Guarded Command Language]], a programming language based on non-deterministic conditionals * [[Guarded suspension]] * [[Iverson bracket]] * [[Logical conditional]] * [[Sentinel node]], an object to represent the end of a data structure * [[Switch statement]] ==References== {{Reflist}} ==External links== * [http://foldoc.org/guard Guard] in ''Free On-Line Dictionary of Computing - FOLDOC'', Denis Howe (editor). * [http://c2.com/cgi/wiki?GuardClause Guard Clause], [[WikiWikiWeb]] * ''The Haskell 98 Report'', chapter [http://haskell.org/onlinereport/exps.html 3 Expressions]. * ''The Mathematica Book,'' section [https://web.archive.org/web/20050408114902/http://documents.wolfram.com/mathematica/book/section-2.3.5 2.3.5 Putting Constraints on Patterns] * ''The Glorious Glasgow Haskell Compilation System User's Guide'', Version 6.4, section [https://web.archive.org/web/20051129140339/http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#pattern-guards 7.3.2. Pattern guards] [[Category:Conditional constructs]] [[Category:Formal methods terminology]] [[Category:Articles with example Haskell code]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Citation needed
(
edit
)
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)