请输入您要查询的百科知识:

 

词条 Gotcha (programming)
释义

  1. Example

  2. See also

  3. References

  4. Further reading

  5. External links

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.{{cn|date=September 2018}}

See also

  • Usability

References

1. ^Gotcha definition at The Jargon File
2. ^[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"]

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}}
  • 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

2 : Computer programming folklore|Programming language folklore

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/11 19:58:55