Department of Declaration Redundancy Department

“And it is a misuse because it goes against the opinions of the language designers.”

Who, as we all know, are infallible.

Sidenote: the D programming language has this too, under the name of auto. Also anonymous classes, anonymous function literals, anonymous nested function literals … it’s very nice for removing redundancy :slight_smile:

–feep, D fanboy

The first BufferedReader is needed to differentiate between that and
this:

ParentClassOfBufferedReader br = new BufferedReader (new FileReader(name));

Here’s a useful observation for you: this is not needed, BufferedReader is implicitly convertible to ParentClassOfBufferedReader. In the extremely rare case when it could really be needed, nothing prevents you from not using automatic type inference.

Why misuse a C# feature? And it is a misuse because it goes against
the opinions of the language designers.

Which is why they added “var” into the language, right?

Get a life.

For simple examples this might be easier, but what about functions that have return values?

var foo = Foo.Bar();

Not very clear what type foo is. Now you have to take the extra step of trying to use foo or hovering over Bar() to find out from intelisense what the type is.

I’m a big fan of consistency over saving a few characters of decleration.

You also want to be more explicit when working with higher level design patterns which are all based on interfaces and subclasses.

Actually although anonymous types might be useful for what they’re used here I’ve found them quite more useful in other situations like:

var row = new
{ Id = reader[“id”].ToString(),
Name = reader[“name”].ToString(),
Age = Convert.ToInt32(reader[“age”]) }

if (row.Age 45 Name.StartsWith(“xxx”)
return “oldie”;
else
return “young”;

or something like that which actually makes (for me) the code more readable.

It is usually painfull to understand someone else code’s.
Code is not only for you and the compiler.

But if you want to compete for the more compact code …
It was a funny game in the 80’s with C, and it is still the case.

(see “The International Obfuscated C Code Contest” :slight_smile:

Yuk. I hate cleaning up code that’s not strictly typed. This stuff is hard enough to make work. Every programmer should wear handcuffs and be required to explicitly type and comment everything.

If programmers had write their own paychecks they wouldn’t write that one-way write only crap code. They would write beautiful code because they never know when they’ll be waist deep in it again.

var = crap

(quote)
var sb = new StringBuilder(256);
var e = new UTF8Encoding();
var md5 = new MD5CryptoServiceProvider();
(/quote)

I agree that these are nice, but I’ve been bitten - already - by maintaining someone elses code where they do something like:

var service = ServiceFactory.Create();

Fantastic. Whats the service? (without running the code - I needed to debug something underneath this). Oh, it’s an ISomething, implemented by whatever the .Create creates.

sure comments would have helped, and maybe the design was wrong, but I found myself cursing the inventor of var at the time.

BTW resharper’s code formatter converts everything to var for you now. Which is kinda nice, in a consistent way if nothing else.

I agree with TSK on this one. BufferedReader could also be Reader. And I take advantage of this facility in OO all the time. I guess the var lovers don’t.

Explicitly declaring the type of a variable is not code bloat or redundant. It is a necesity in many cases. I will never use var. It only makes things less clear and causes the possibility of the compiler choosing the wrong class type on my behalf. Sure, I could use it when there is no ambiguity. But why have to make the choice. And how do I know that although there may be no ambiguity now, that there won’t be ambiguity in the future.

I never buy arguments that are centered around reducing typing. The amount of typing saved in this instance is a one time cost and significantly less that the amount of time that could be wasted tracking down errors in the future.

Indeed, Nic Wise, I absolutely hate when other people use it to store the result of a function call. Code isn’t just read in an IDE with Intellisense available. I personally only ever use ‘var’ in LINQ, and anonymous types in general (which, to be honest, I don’t particularly like much outside of LINQ either).

IMO Var opens up the possibility of incredible messes and maintenance hell, all to save half a second of typing. Is it really worth using it?

Taking the example, I would really prefer this:

StringBuilder sb = new(256);
UTF8Encoding e = new();
MD5CryptoServiceProvider md5 = new();

because it is easy to directly see what is the type of the variable

It also permit to have a

Set setOfHelper = new HashSet();

What about inherited types?
Base b = new Subclass();
How can you shorten this?

Also, Philippe said:
"However, for simple types, I don’t agree…
var i = 1;
just feels wrong."
It should feel wrong. The ‘1’ can be a byte, sbyte, short, ushort, int, uint, long, ulong. It makes a difference what type it is.

With the IDE helping you type with CTRL-space, why shouldn’t you type the whole thing.

Side note: my longest variable name in production code was:
TimeOfFlightToObjectInFourcoseconds

P.S. A fourcosecond is equal to 4 picoseconds.

I’m not a fan of the overuse of var and honestly, I think it was introduced in to the language to let linq and anon types work. Without linq, I don’t think we’d have seen this language feature.

I’ve been round and round in circles on this for the past 8-6 months, and I really feel that it hinders readability if you’re ever assigning from method calls, when you’re using interfaces, and makes variable declarations look messy and unstructured (this, however, is a matter of taste).

It always feels like in the long run, adding dynamic language features to staticly typed languages reduces the usefulness of compile time checking.

I believe code should be practically self documenting in its readability, and I think that using generic declariations like var or VBs Dim reduces this quality.

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

  2. var maxentries = 5;

  3. var pi = 3.14159;

  4. string or url? is it obvious? no.
    Yes, it is a string.

  5. int or long? is it obvious? no.
    Yes, it is a int. A long would be written as “5L”

  6. float or double? is it obvious? no.
    Yes, it is a double. A float would be written as “3.14159f”

So I disagree that the declarations are unclear, but I would probably still explicitly declare the type for primitives.

You should give Python a shot - you don’t have to deal with any of this unnecessary verbosity.

‘var’ is not redundant, it indicates you’re declaring a new variable, and not simply making an assignment to an existing variable.
Scala has a powerful type inferencer, with var and also val (for values that won’t change).
I know, nobody asked.

var is just about as useful as Option Strict Off…

@masklinn:
Regarding the Foo/Bar example - I agree that the compiler should realize that I’m not calling any Bar-specific stuff, but it doesn’t. Trying to compile that example fails.

In VB, you really have a choice with verbosity.
Dim r as New SQLDataReader()
or
Dim r as IDataReader = New SQLDataReader()

We can repeat ourselves if we want to, but only if necessary.