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
Gotcha (programming)
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|Code that is valid but counter-intuitive}} In [[computer programming|programming]], a '''gotcha''' is a valid construct in a system, program or [[programming language]] that works as documented but is [[counter-intuitive]] and almost invites mistakes because it is both easy to invoke and unexpected or unreasonable in its outcome.<ref name=jf/> == Example == The classic gotcha in [[C (programming language)|C]]/[[C++]] is the construct <syntaxhighlight lang="c"> if (a = b) code; </syntaxhighlight> It is [[syntactically]] valid: it puts the value of <code>b</code> into <code>a</code> and then executes <code>code</code> if <code>a</code> is non-zero. Sometimes this is even intended. However most commonly it is a typo: the programmer probably meant <syntaxhighlight lang="c"> if (a == b) code; </syntaxhighlight> which executes <code>code</code> if <code>a</code> and <code>b</code> are equal.<ref name=jf>[http://catb.org/jargon/html/G/gotcha.html Gotcha definition at The Jargon File]</ref> Modern [[compiler]]s will usually generate a warning when encountering the former construct (conditional branch on assignment, not comparison), depending on compiler options (e.g., the <code>-Wall</code> option for [[GNU Compiler Collection|gcc]]). To avoid this gotcha, some programming languages such include specific syntax for when this is desired behavior, such as [[Python (programming language)#Expressions|Python's "walrus" operator]] (<code>:=</code>). In languages where this specific syntax does not exist, there is a recommendation<ref>[https://www.securecoding.cert.org/confluence/display/c/VOID+EXP21-C.+Place+constants+on+the+left+of+equality+comparisons "VOID EXP21-C. Place constants on the left of equality comparisons"]</ref> to keep the [[Constant (programming)|constants]] in the left side of the comparison, e.g. <code>42 == x</code> rather than <code>x == 42</code>. This way, using <code>=</code> instead of <code>==</code> will cause a compiler error (see [[Yoda conditions]]). Many kinds of gotchas are not detected by compilers, however.{{cn|date=September 2018}} ==See also== * [[Usability]] ==References== {{Reflist}} ==Further reading== * {{cite book|author=Stephen C. Dewhurst|title=C++ Gotchas (Avoiding Common Problems in Coding and Design)|publisher=Addison-Wesley|date=2003|isbn=0321125185}} ==External links== {{wiktionary|gotcha}} * [http://www.literateprogramming.com/ctraps.pdf C Traps and Pitfalls] by Andrew Koenig * [https://web.archive.org/web/20160817045649/http://buhoz.net/public/books/lenguajes/c++/tecnicas/C++.Gotchas-.Avoiding.Common.Problems.in.Coding.and.Design.pdf C++ Gotchas] A programmer's guide to avoiding and correcting ninety-nine of the most common, destructive, and interesting C++ design and programming errors, by Stephen C. Dewhurst [[Category:Computer programming folklore]] [[Category:Programming language folklore]] [[Category:Programming language design]]
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:Cite book
(
edit
)
Template:Cn
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Sister project
(
edit
)
Template:Wiktionary
(
edit
)