Department of Declaration Redundancy Department

Tom

Not trying to prove anything, simply stating facts. In my opinion var is less readable, but readability is subject by nature. And I would suggest that the more explicit code is the easier it would be to maintain, but again that’s just my opinion. If anyone has a measure of readability or maintainability that is not subjective I’d love to see it.

I also think that is a great new feature in C#. Before I always felt that the VB.Net syntax

Dim reader as new FileReader(“blabla”)

is far superior. Not that I would ever use VB.Net for anything serious though :slight_smile:

@David Fauber

‘Seems like this is going in circles. I remember back when VB6 was what most business apps were written in and the fact that the compiler didn’t make you specify when you were allocating a variable was seen as some great evil and one of the reasons VB was a “toy language”.’

I remember when the weak typing of Perl was considered a great win. And then it wasn’t anymore, and Java’s inheritance-based type system was considered the salvation of development. Then Ruby’s dynamic typing was the key to its productivity. Round and round…

What do you mean by redundancy?

@Jeff Atwood
But why can’t the 1 simply grow/size to fit? Wouldn’t that be preferable to the overspecific data typing and the inevitable integer overflow weirdnesses errors and even exploits this results in?

Yes, for an entire class of apps, you would be better off using a simple, unbounded Number type, calculated and rounded using decimal rules instead of binary, so that math behaves like people expect it to. This is available in some languages, although they are “much slower” than native numeric types; although whether that’s “too slow” is another question entirely.

But sometimes specifying the width/representation is a requirement or a distinct advantage (usually in size/speed).

The new C++ standard (C++0x) seems to have moved towards this with “auto”, so instead of this:

vectorstring::const_iterator it = names.begin();

you will be able do this:

auto it = names.begin();

Assuming someone actually implements it :wink: Although I do wonder how this plays with inheritance. e.g. Would var be the most generic type available or always what you tell it to be (I guess the latter, since it makes most sense).

Ah, I hope “” and “” aren’t eaten by the post demon. Otherwise the vector was of strings there.

@Dave: If the constructor fails the variable will always be null and useless to you, so it is correct to not even have access to it.

I agree that in the examples above, but when it comes to variables being assigned through a call to a function (something like var x = GetSomeCrazyStuff(x,y)) the data type in front of the variable makes it easier to figure out what GetSomeCrazyStuff is supposed to return.

Amen! Now we just need to get rid of “var” :wink:

Gets even worse with generics. Nothing like typing

MapString, MapLong, Point bla = new MapString, MapLong, Point();

Agreed. I use var all the time. It could be even more concise if they dropped the “var”

url = “http://tinyurl.com/5pfvvy”;
maxentries = 5;
pi = 3.14159;
n = new int[] {1, 2, 3};

Another favorite is the ?? operator (null coalescing operator)

Console.WriteLine(name ?? “it’s null dude”);

Bleh, comment software ate my less than and greater than signs. One more try:

MapString, MapLong, Point bla = new MapString, MapLong, Point();

Someone wrote excellent article exactly about this in 2003 when Bruce Eckel and then Jeff Atwood was blogging about how hot Python is.

read his reaction, it’s very very thoughtful
http://www.xoltar.org/old_site/misc/static_typing_eckel.html

As one of the few, remaining C/C++ developers left - I think this is the worst idea in the world. Why? its completely non readable and makes maintenance a bitch. Being lazy is not a good thing! Don’t be a lazy programmer!!

end rant

Also,

myEvent += new MyEventHandler(myEventHandler);

can be reduced to:

myEvent += myEventHandler;

Any particular interest to “Judy York”?

url = “http://tinyurl.com/5pfvvy”;

“Anything that removes redundancy from our code should be aggressively pursued”

Actually, that’s a pretty strong claim that would need justification. Many people argue that redundancy in code is a good thing because it helps catching logical errors in the code – provided the compiler checks all the information; else the redundancy is obviously harmful (because the information might differ).

That being said, I agree with you in this case. Redundancy in variable declaration is useless code bloat. I actually prefer the new notation for another reason: because it prefixes declarations with a keyword, analogously to VB’s ‘Dim’ statement. I’ve always hated C-like languages for their implied declarations which make the code much harder to parse (even for a human reader).

“You might even say implicit variable typing is a gateway drug to more dynamically typed languages. And that’s a good thing.”

You might even say implicit variable typing is a gateway drug to Hindley-Milner type inference in statically typed languages. :wink:

Yes, lets remove the variable declaration phrase, that way the compiler has no way to lets us know when we’ve mistyped a variable name. /sarcasm

Don’t need ‘var’ either. Of course it’s a variable.