2010-04-26

Yes, it is C. Smaller scope better scope

Once upon a time there lived a paradigm (sorry, that is an ugly word, but I don't know better) which told the programmer to declare all the variables at the beginning of the function. This paradigm was enforced by the compiler in Pascal, C and many other languages. You can guess. It was easier to write compilers that way. Then came the new winds with C++ (maybe it wasn't the first) which introduced RAII (Resource acquisition is initialization) which gave us three brand new concepts at once.
  • Variable scoping
  • Lifecycle management by scope (calling destructor when execution reaches the end of it's scope)
  • Bounding lifecycle of a variable to an object
Many may not know but since C99 the first is also available in C. A trivial example of it is the in-place declaration of iterator in for.
for(int i=0; i<10; ++i){ printf("%d^2 = %d\n",i,i*i); }
The new paradigm (woof) is that a variable should have the smallest scope possible to ease reading and interpretation of code (by humans).

Next: Exit strategy: Single exit point can be easily bad.

No comments:

Post a Comment