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. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
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 premises, leads to a seemingly self-contradictory or a logically u ...
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++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
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 translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs tha ...
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, there is a recommendation"VOID EXP21-C. Place constants on the left of equality comparisons"
/ref> to keep the constants 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). Many kinds of gotchas are not detected by compilers, however.


See also

* Usability


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