The Full Wiki

Gotcha (programming): Wikis


Note: Many of our articles have direct quotes from sources you can cite, within the Wikipedia article! This article doesn't yet, but we're working on it! See more info or our list of citable articles.


From Wikipedia, the free encyclopedia

In programming, a gotcha is a feature of a system, a program or a programming language that works in the way it is documented but is counter-intuitive and almost invites mistakes because it is both enticingly easy to invoke and completely unexpected and/or unreasonable in its outcome.


Gotchas in the C programming language


Equality operator

The classic gotcha in C is the fact that

if (a=b) code;

is syntactically valid and sometimes even correct. It puts the value of b into a and then executes code if a is non-zero. What the programmer probably meant was

if (a==b) code;

which executes code if a and b are equal. To avoid this gotcha, it is recommended to keep the constants in the left side of the comparison, i.e. 42 == x rather than x == 42. This way, using = instead of == will flag a compiler error warning the programmer about an attempt to assign a value to a constant.

Function calls

Example from C and relatives:

/* File name: fourtytwo.c */
foo() {
   printf("  42!\n");
int main() {
    printf("I will here below write '42':\n");
    printf("I think I wrote '42', but I'm not quite sure...\n");
    return 0;

will produce (when compiled and run):

$ ./fourtytwo
I will here below write '42':
I think I wrote '42', but I'm not quite sure...

What foo; contraintuitively does is returning the address of the function foo and putting it on the execution stack. Then the stack is emptied. What the programmer intended was probably instead the function call:


where the parantheses indicates a function call with zero arguments.

Gotchas in the C++ programming language

Initializer lists

In C++, it is the order of the class inheritance and of the member variables that determine the initialization order, not the order of an initializer list:

#include <iostream>
class CSomeClass
  CSomeClass(int n)
   std::cout << "CSomeClass constructor with value ";
   std::cout << n << std::endl;
class CSomeOtherClass
  CSomeOtherClass() //In this example, despite the list order,
  : obj2(2), obj1(1) //obj1 will be initialized before obj2.
   //Do nothing.
  CSomeClass obj1;
  CSomeClass obj2;
int main(void)
 CSomeOtherClass obj;
 return 0;

Gotchas in Ruby programming language


Scope of a local variable starts at its definition and ends at the end of the method. Within its scope, the local variable obscures current object's methods.

irb(main):001:0> class Blah
irb(main):002:1>   def foo
irb(main):003:2>     puts "I'm foo, and I got called"
irb(main):004:2>   end
irb(main):006:1*   def bar
irb(main):007:2>     if false then
irb(main):008:3*       foo = 1
irb(main):009:3>     end
irb(main):010:2>     puts "I am about to call foo"
irb(main):011:2>     foo
irb(main):012:2>     puts "I believe I called foo"
irb(main):013:2>   end
irb(main):014:1> end
=> nil
irb(main):015:0> x =
=> #<Blah:0xb7d3b5b4>
I am about to call foo
I believe I called foo
=> nil

Iterating Range

Ruby has a data type called Range; an object of this type constitutes a set of everything between the start and end of the range (including or not including the boundaries, depending on additional conditions). Since Range is a subclass of Enumerable, one would intuitively expect that iterating a valid Range object will give you every single object in that set, from start to end. This expectation turns out to be incorrect:

irb(main):001:0> (1..3).each { |i| puts i }
=> 1..3
irb(main):002:0> (3..1).each { |i| puts i }
=> 3..1

Returns from the block

return statement returns from the context it was defined in, not from the context it is being executed in. For example:

def boo()
  transaction do
    return if(something)

One might expect that return would allow the transaction to be properly closed, but unfortunately that's not the case -- the code in transaction() taking care of that will never be executed if something==true.

Implicit exception catching

rescue => err

One would expect that the rescue block would catch any exception thrown, but this is not true -- it will only catch RuntimeError (which is a subclass of StandardError, which in turn is a subclass of Exception). If you intend to catch all exceptions indeed, the following syntax should be used:

rescue Exception => err

Implicit array creation

Under certain circumstances, comma implicitly binds the values into an array:

irb(main):001:0> 1, 2
SyntaxError: compile error
(irb):1: syntax error, unexpected ',', expecting $end
        from (irb):1
irb(main):002:0> a = 1, 2
=> [1, 2]

So, certain typos that would be immediately noticed in other languages, would not trigger a syntax error in Ruby and may lead to some time-consuming investigations:

irb(main):001:0> def hello
irb(main):002:1>   a = 1
irb(main):003:1>   b = 2,
irb(main):004:1*   c = 3
irb(main):005:1>   d = 4
irb(main):006:1>   puts "This is my program that will say Hello to the world!"
irb(main):007:1>   if a == 1 then
irb(main):008:2*     if b == 2 then
irb(main):009:3*       if c == 3 then
irb(main):010:4*         if d == 4 then
irb(main):011:5*           puts "Hello World!"
irb(main):012:5>         end
irb(main):013:4>       end
irb(main):014:3>     end
irb(main):015:2>   end
irb(main):016:1>   puts "My program has said Hello!"
irb(main):017:1> end
=> nil
irb(main):018:0> hello
This is my program that will say Hello to the world!
My program has said Hello!
=> nil

Gotchas in DOS batch file language

One would expect that the batch file would execute commands exactly as if they were typed manually. This is not true in the particular case when one of the commands is determined to be another batch file, in which case it simply replaces the one currently being executed.

C:\test>copy con main.bat
@echo off
echo Starting main.bat
echo Completing main.bat
        1 file(s) copied.

C:\test>copy con subr.bat
@echo off
echo Starting subr.bat
echo Completing subr.bat
        1 file(s) copied.

Starting main.bat
Starting subr.bat
Completing subr.bat


External links


Got something to say? Make a comment.
Your name
Your email address