Print

Print


On Mon, 26 Oct 2009, Nick Husher wrote:

> // C-like language:
> int i = 5;
> if(1==1) {
> 	int i = 10;
> 	printf("%d\n", i); // prints "10"
> }
> printf("%d\n", i); // prints "5"
>
> // Javascript
> var i = 5;
> if(true) {
> 	var i = 10;
> 	console.log(i); // prints "10"
> }
> console.log(i); // prints "10"

Everyone's notion of what is intuitive is different. To me the C behavior
is entirely intuitive. I find the Javascript example to be rather odd. I
interpret 'int i = 10' as the creation of a new variable. I don't want
modifications to this variable to impact other variables in a more global
scope (that I might not know about) just because they happen to have the
same name. To me that's a recipe for disaster.

#include <someheader.h>  // Contains a declaration of 'i'.

void f()
{
  if (x == y) {
    int i = 10;  // This must not modify any other variable named 'i'
    // etc...
  }
}

When I assign 10 to my 'i' the last thing I want to worry about is the
possibility that I might have modified a global variable defined in a
header I didn't write.

Peter