Gotcha (programming)
   HOME

TheInfoList



OR:

In programming, a gotcha is a valid construct in a system, program or
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
that works as documented but is
counter-intuitive A paradox is a logically self-contradictory statement or a statement that runs contrary to one's expectation. It is a statement that, despite apparently valid reasoning from true or apparently true premises, leads to a seemingly self-contradictor ...
and almost invites mistakes because it is both easy to invoke and unexpected or unreasonable in its outcome.


Example

The classic gotcha in C/ C++ is the construct if (a = b) code; It is syntactically valid: it puts the value of b into a and then executes code if a is non-zero. Sometimes this is even intended. However most commonly it is a typo: the programmer probably meant if (a

b) code;
which executes code if a and b are equal.Gotcha definition at The Jargon File
/ref> Modern
compiler In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
s will usually generate a warning when encountering the former construct (conditional branch on assignment, not comparison), depending on compiler options (e.g., the -Wall option for gcc). To avoid this gotcha, some programming languages such include specific syntax for when this is desired behavior, such as Python's "walrus" operator (:=). In languages where this specific syntax does not exist, there is a recommendation"VOID EXP21-C. Place constants on the left of equality comparisons"
/ref> to keep the
constants Constant or The Constant may refer to: Mathematics * Constant (mathematics), a non-varying value * Mathematical constant, a special number that arises naturally in mathematics, such as or Other concepts * Control variable or scientific const ...
in the left side of the comparison, e.g. 42

x
rather than x

42
. This way, using = instead of

will cause a compiler error (see
Yoda conditions In programming jargon, Yoda conditions (also called ''Yoda notation'') is a programming style where the two parts of an expression are reversed from the typical order in a conditional statement. A Yoda condition places the constant portion of th ...
). Many kinds of gotchas are not detected by compilers, however.


See also

*
Usability Usability can be described as the capacity of a system to provide a condition for its users to perform the tasks safely, effectively, and efficiently while enjoying the experience. In software engineering, usability is the degree to which a softw ...


References


Further reading

*


External links

{{wiktionary, gotcha
C Traps and Pitfalls
by Andrew Koenig
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 Computer programming folklore Programming language folklore Programming language design