Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

From documentation:

 

� Implicitly typed local variables, which permit the type of local variables to be inferred from the expressions used to initialize them

 

var i = 5;

var s = "Hello";

var d = 1.0;

var numbers = new int[] {1, 2, 3};

var orders = new Dictionary<int,Order>();

 

Is it me or does this take maintainability way down (especially when you through in a few 10's to 100's line of other code - what was s again? Now we get back to declaring objects:

 

strUser;

intID;

objWorkflow

decAmount;

 

Yeah - it's a pain enough to deal with in JavaScript - I don't really want to have to support this in compiled code too! Readability goes threw the floor, and if somebody has commented well or has a poor naming convention - good luck buddy!

 

Also they don't mention what happens if you do this:

 

var s = "Sam";

//50 lines of code

s = 0D;  //notice I changed it to decimal

 

Does it throw a compile error? a run-time error? accept it?

 

Listen, I'm all about new features for increased output, but honestly - this is a step backwards when you think that a majority of the cost of an enterprise application isn't spent initially building it, but maintaining and extending it over time.

 

In fact I'd really like to see what all the 'features' of C# 3.0 are truely for - the way C# works now is pretty darn good IMNSHO and I'm a big believer in 'if it ain't broke don't fix it'. What are the business objectives being solved by these new features? Who really benifits? What is it doing now that can't be done currently? Is the current work arounds that these are solving that much trouble? How much of maintnance nightmare is going to cause? In otherwords a problem now I know what to look for, but now if that problem can be caused by doing some 1.1 way, 2.0 way, and 3.0 way - I have to look for 3 differant programming styles for a bug rather than one.

Edited by bri189a
Posted

It's not as dire as you paint it. :)

 

All "var" does is cut down on clutter really. e.g.

MyVeryLongGenericTypeName<MyVeryLongClassName> foo = new MyVeryLongGenericTypeName<MyVeryLongClassName>(...);

becomes

var foo = new MyVeryLongGenericTypeName<MyVeryLongClassName>(...);

The type of the variable is determined by the initializer and cannot be changed (yes, it would be a compile-time error). Just like any other variable, if you hover your mouse over it you'll see it's type.

"Who is John Galt?"
  • *Experts*
Posted

LOL:

These complaints about the new C# features are the exact same ones people always have about changes to any language. Scan the posts in "Random Thoughts" for complaints on the changes between VB6 and VB.NET. Substitute the VB6/VB.NET complaints with C#1 and C#3 and the posts are identical. Here's a rephrased quote that works for any language upgrade - this should probably be made a sticky:

I'd really like to see what all the 'features' of [new language] are truely for - the way [current language] works now is pretty darn good IMNSHO and I'm a big believer in 'if it ain't broke don't fix it'. What are the business objectives being solved by these new features? Who really benifits? What is it doing now that can't be done currently? Is the current work arounds that these are solving that much trouble? How much of maintnance nightmare is going to cause?

The simple answer, as usual: if you don't like the new features then don't use them! If you're worried that others are using them, then stop worrying - you'll never be able to change how others program unless that's your job.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • Leaders
Posted
LOL:

These complaints about the new C# features are the exact same ones people always have about changes to any language.

 

I'd really like to see what all the 'features' of [new language] are truely for - the way [current language] works now is pretty darn good IMNSHO and I'm a big believer in 'if it ain't broke don't fix it'. What are the business objectives being solved by these new features? Who really benifits? What is it doing now that can't be done currently? Is the current work arounds that these are solving that much trouble? How much of maintnance nightmare is going to cause?[/Quote]

It would probably be a good experience to go back to those posts and try to answer those questions. I honestly don't know how able we will be to answer them satisfactorily.

 

The simple answer, as usual: if you don't like the new features then don't use them! If you're worried that others are using them, then stop worrying - you'll never be able to change how others program unless that's your job.

-ner

You are very close to contradicting yourself. Fill in some gaps and the logic clashes. As usual, the simple answer is nonsense, and I don't doubt that you know it. You can not participate in a programming community without having the same base skill set as the rest of the community, and it becomes exceedingly difficult to either make a living programming, or to make an interesting hobby of programming, without participating in programming communities. If you want your programming to be purposeful, you need to be part of the community, and you need to know the new features, hence we can not simply not use them!

[sIGPIC]e[/sIGPIC]
Posted
If you want your programming to be purposeful' date=' you need to be part of the community, and you need to know the new features, hence we can not simply not use them![/quote']Sure you can! You might be silly for doing so, but if you are more comfortable doing something in a certain way and you have no reason to learn the new way, then why make the switch? I agree that you should at least know about the new stuff -- as many features as possible for that matter -- as it will only make you a better developer. But it is by no means a requirement that everyone use or not use implicit variable declarations, for example. You'll still write code that is just great and works perfectly well. What will I be doing? I'll be learning the new stuff and learning when it is appropriate to use.

 

Here's another example...I never use Do-While loops. I just don't like them. I much prefer while loops and formulate my loop invariant and code flow to work with them instead of do-while. I guarantee my code does not suffer for it.

  • Leaders
Posted
I suppose I should point out that even if we don't use the new features we still need to fully understand them, and new programmers still need to learn them, and if we want to use other people's code, there is a good chance that we will need to use the new features.
[sIGPIC]e[/sIGPIC]
Posted (edited)

Good point Nersus. I'll go gripe elsewhere :)

 

However I still say it will be a pain to maintain if you don't have Intellesense.

Edited by bri189a
Posted
However I still say it will be a pain to maintain if you don't have Intellesense.

 

So why wouldn't you? ;)

 

I'd say there's no excuse not to use a VS grade IDE or equivalent these days.

"Who is John Galt?"
Posted

From what I've seen, you wouldn't be the first to dislike this prospective "feature". I can tell you that I've literally wasted days cleaning up crappy VB6 code where junior programmers thought "variant" was a great idea. Non-typed variables was already a bad idea a decade ago.

 

I seriously doubt it if this feature makes it to release.

Posted
I really don't see what the big deal is. This is just a way to make potentially difficult to read code much easier. See IngisKahn's post above. Strong typing doesn't suddenly dissapear and it's not like you can't see what you declared it as just as easily whether the type is in the front of the statement or the back. Really, the only problem I see is if you var something and then initialize it incorrectly -- intend to use an int and declare a float or vice versa. But why you would type v-a-r instead of i-n-t in that case is beyond me. I don't see maintainability as an issue. You could shoot yourself in the foot, but var probably shouldn't be used for primitives anyway.
Posted
I really don't see what the big deal is. ... I don't see maintainability as an issue. You could shoot yourself in the foot' date=' but var probably shouldn't be used for primitives anyway.[/quote']

 

For most of us interested in software development and reading these posts, it's not personally a big deal since we're smart enough to know when to use it and why.

 

The problem is that it reduces the code quality of sub-programmers to zilch. If you never have to deal with one of these monkeys, then fine. I just know that within a year of this feature, I'll encounter an idiot who did this inappropriately all over the place.

 

Any of you who worked with many VB programmers in the 90's know exactly what I'm talking about - there are some who type semi-randomly and hope for the best until by chance the compiler gives them something that "runs" - if you let them be too lazy to have to enter the type, that's exactly what they'll do.

  • Leaders
Posted

With the combination of implicitly typed variables and object initializers we get:

var x = new { UpperLeft = new Point { X = 0, Y = 0 }, LowerRight = new Point { X = 5, Y = 5 } };

Anonymous types! Alright! We are declaring a variable of type... um... It's not that hard to figure out, but this is also a pretty simple example, and it isn't exactly intuitive. I certainly prefer:

Rectangle X = new Rectangle(0, 0, 5, 5);

I can't really seeing anonymously typed variables being used much, but when they are... well, there's a disaster waiting to happen. I hope that anonymous types don't make the 3.0 cut, and I would prefer that implicit typing be restricted to initializers using the new keyword (hence the variable type will be there clear as day either way), but if not, it's not the end of the world. I just can't see the benefit of typing "var" instead of "int" or "bool".

[sIGPIC]e[/sIGPIC]
Posted (edited)

Exactly - if:

 

MyClassName x = new MyClassName()

 

is so difficult that you have to:

 

var x = new MyClassName()

 

And for good measure, if you have a class name that's more than 8-12 characters it problems needs a name refactoring anyway.

 

What are you really gaining? It's not like you could just type M-y-C and then press Alt-Space and have it auto complete or narrowed down pretty far.

 

And as Jaco posted I work with plenty of ex VB6 programmers who made it into VB.NET (kicking and screaming) who still use #include in their ASP.NET projects (another example of something that is allowed that shouldn't be) - and let me say from experiance, his description of 'monkeys' and throwing crap together until the compiler lets it compile is a very accurate description. I wish I was priveledged enough not to have to deal with outsourced sub-par programmers (and I fully admit I'm not the best either...) - but until executives figure out they're spending more money supporting poorly written code than if they would've in-sourced or at least spent the extra to out-source worthy programmers, I will have to deal with these types.

 

I can see already some idiot right a function that looks like this:

 

public bool DoSomething(var x, var y, var z)

{

var a = x.CreateSomething()

var b = y.DoSomething(z)

 

return a.Modify(b)

}

 

Yeah, how's that for readability????? Support that when it's called 15 nested functions in!!!

 

Maybe type arguements can't be 'var' types...I don't know, but this is the kind of mess these people Jaco mentioned make - oh and it's written in VB with Option Strict/Explicit turned off to make it all the more fun.

 

Well said Jaco and way to bring my point home!

Edited by bri189a
Posted

marble brings up anonymous types which is whole different story (and the one 3.0 feature that I feel a bit uneasy about).

 

And bri, come on! You wrote it yourself: implicit typing is for local variables only. How could a type safe language call a function on a variable without a type? And restricting names to 12 characters??? Good luck, especially with generic types.

"Who is John Galt?"
Posted

You're right, I got off on a tangent and knee-jerked reacted - I just get stired up real easy, but the 12 characters thing...my point was if that you have:

 

MyClassForGettingSomeCommonFunctionality m = new MyClassForGettingSomeCommonFunctionality

 

(which I have) - it should have it's name refactored. And in my view, var is just a shortcut for you to not to have to type all of that.

 

I personally have written very few classes that are longer than 12 characters...in the above that gets you to the 'MyClassForGe' point - I'd say 20 is the abosulte tops before just being over-descriptive - comments and class descriptions should have a place - in the help file. A class name should be descriptive, but to the point.

Posted
Having such long names would certainly be commonplace whilst referencing objects that are in a namespace not included in the using section of a page (or in order to avoid clashes with the same name in differen't namespaces). For example I'm often writing System.Drawing.Rectangle as its the only place in a class I use something from that namespace. Combine this with a particularly long named namespace or class and you soon get very long names very quickly.
Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted
marble brings up anonymous types which is whole different story (and the one 3.0 feature that I feel a bit uneasy about).

Well, lemme explain why I think that anonymous types fall within the scope of this conversation. It might be possible to use anonymous types without implicit typing...

Rectangle MyRect = new {UpperLeft = New Point{X = 0, Y = 0}, BottomRight = New Point{X = 5, Y = 5}};

Yeah, that hurts my brain a little, but I can make sense of it.

When combined with implicit typing, however...

var Skoobideebop = new {UpperLeft = New Point{X = 0, Y = 0}, BottomRight = New Point{X = 5, Y = 5}};

we achieve a whole new level of unreadability. If anonymous types are vinegar, implicit typing is baking soda.

 

 

But now that I'm on the subject anonymous types, it seems more than just a little inefficient to me. Look at my second code example. (This is what I found when researching on .Net 1.1, maybe, hopefully, newer versions are better optimized.) When you break down the IL, you find that you are actually creating six structs, initializing them, calling three constructors, and performing three copy operations. Now we are using properties instead of parameters to initialize our structs (because new syntax lends itself to this), adding six function calls to set property values. This is a bit of overkill to create a rect. I don't expect this to happen for a rectangle, but when people begin to tailor their classes and structs to C# syntax, which I believe they will, in certain situations it could lead to loads of anonymous types, increasing cpu usage for data initialization tenfold. It might make your code very readable, but it could also kill you in any kind of data processing if you don't know what's going on under the hood. And now I'm done complaining. Sorry I got off topic.

[sIGPIC]e[/sIGPIC]
Posted (edited)

var Skoobideebop = new {UpperLeft = New Point{X = 0, Y = 0}, BottomRight = New Point{X = 5, Y = 5}};

First off, naming a variable Skoobideebop makes it unreadable. I think you're tyring to make a case for not using something by coming up with the worst things someone could do. Instead of blanket discouragement, we should be trying to come up with a best uses policy that works for everyone. "Don't use var with generics" sounds like a good one to me. It doesn't say "don't ever use var becuase someone might use it with a generic and make code unreadable" it simply advises against using var with generics which seems like a great idea.

 

So far we have:

1. Var shouldn't be used with primitives. This avoids possible mis-decleration.

var area = 25; //did I actually want a float here?

2. Var shouldn't be used with anonymous types. Var with anonymous types is extremely difficult to read.

var myRect = new {UpperLeft = New Point{X = 0, Y = 0}, BottomRight = New Point{X = 5, Y = 5}}; //uuhh...that's a rectangle, I think.  Lemme decihper that.

3. Use Var in cases when initializing a class with extremely long (>25 characters) class names. If the decleration goes off the page or onto several lines it is not as readable as it could be.

MyVeryLongGenericTypeName<MyVeryLongClassName> foo = new MyVeryLongGenericTypeName<MyVeryLongClassName>(...); //something.something.something.superlongclassname is another example of a long class name
//becomes
var foo = new MyVeryLongGenericTypeName<MyVeryLongClassName>(...); 

4. Continue to use good variable naming conventions with Var and realize that Var is still strongly typed. You don't just throw out all your good practices becuase you've learned something new.

 

I'm kind of going out on a limb with this to try and make something positive come out of this thread. Is there anything else? Can we make these better? Do some of these not belong?

Edited by mskeel
  • Administrators
Posted

The code snippet you use in point 2 is more to do with anonymous types than generics, from what I have seen I would tend to stay clear of them in general anyway.

Using var with generics doesn't seem to be too bad of an idea and can remove a level of duplication that can result in easy mistakes later

//following requires the type to be specified twice, if int needs to be changed to long
//for example I need to change it in two locations
MyShortName foo = new MyShortName();

//only declare the type once
var foo = new MyShortName();

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

A good point about generics and very pragmatic.

 

I fixed my post above. I meant to write 'anonymous' as that's what marble_eater was talking about, but I must have had generics on the brain while typing. sorry about the confusion.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...