Gotcha (programming)

In 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.[1]

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.[1] Modern compilers will usually generate a warning when encountering the former construct (conditional branch on assignment, not comparison), depending on compiler options (e.g., -Wall option for gcc). To avoid this gotcha, there is a recommendation[2] 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

References

Further reading

  • Stephen C. Dewhurst (2003). C++ Gotchas (Avoiding Common Problems in Coding and Design). Addison-Wesley. ISBN 0321125185. 

External links

  • 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

  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.


Gotcha_(programming)
 
Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.



Manage research, learning and skills at defaultLogic. Create an account using LinkedIn or facebook to manage and organize your IT knowledge. defaultLogic works like a shopping cart for information -- helping you to save, discuss and share.


  Contact Us