33 Replies - 9396 Views - Last Post: 14 November 2012 - 06:46 PM
#1
To 'var' or not to 'var'?
Posted 13 December 2011 - 04:25 AM

POPULAR
I found this debate but opinions there are as split-minded as they can be?
So your thoughts on the var keyword?
Replies To: To 'var' or not to 'var'?
#2
Re: To 'var' or not to 'var'?
Posted 13 December 2011 - 05:49 AM
I think the misuse of var is when it used for virtually every variable definition.
#3
Re: To 'var' or not to 'var'?
Posted 13 December 2011 - 05:59 AM
#4
Re: To 'var' or not to 'var'?
Posted 13 December 2011 - 06:07 AM
#5
Re: To 'var' or not to 'var'?
Posted 13 December 2011 - 06:51 AM
#6
Re: To 'var' or not to 'var'?
Posted 13 December 2011 - 07:52 AM
#7
Re: To 'var' or not to 'var'?
Posted 13 December 2011 - 08:15 AM
For example: If you have a car class, and several inherited classes from that like truck, sedan, convertible
When you retrieve the type of var the user has it might be any of them. So how do you code for it? Use a var.
#8
Re: To 'var' or not to 'var'?
Posted 13 December 2011 - 08:33 AM
Sergio Tapia, on 13 December 2011 - 07:51 AM, said:
Yes, we did.
http://www.dreaminco...do-you-use-var/ (please read this one, several of us have already explained our positions there)
http://www.dreaminco...-var-vs-object/
As I've said before, I always use var. There's no reason not to. We have modern IDEs that can tell us type information just from hovering over an identifier name. Why bother being redundant if you don't have to?
And remember, var isn't dynamic. It's just implicit. It's still a strong type, it's just shorter to type.
#9
Re: To 'var' or not to 'var'?
Posted 13 December 2011 - 08:45 AM
I'm still using var in LINQ, but I've also started using it when I simply can't be bothered typing out full names like ThisIsAReallyLongClassName longClass = new ThisIsAReallyLongClassName();.
The problem with this is that it feels like mixing code conventions when you see something like this, for example:
Socket mySocket = new Socket(); var longClass = new ThisIsAReallyLongClassName(); IPAddress myIP = new IPAddress();
I'm still torn..
On the other hand though, I do stick by what I said about using it in ambiguous places, it shouldn't be done. For example when a method is returning an object of a certain type but not made clear.
This post has been edited by RudiVisser: 13 December 2011 - 08:46 AM
#10
Re: To 'var' or not to 'var'?
Posted 13 December 2011 - 08:59 AM
#11
Re: To 'var' or not to 'var'?
Posted 13 December 2011 - 11:22 AM
So the thing that has been agreed is:
'var' should be used in LINQ
'var' should be used when long class names are involved
'var' should be used when needed in foreach loops
'var' should be used for 'mumble' types
'var' can be used to declare a type that is being returned from the method (since the IDE can tell us what that type is {just in terms of readability})
'var' should not be used in simple declarations (like int i = 4 NOT var i = 4 or string s = "Hello World!" NOT var s = "Hello World")
So are minds set on this?
This post has been edited by RexGrammer: 13 December 2011 - 11:23 AM
#12
Re: To 'var' or not to 'var'?
Posted 13 December 2011 - 11:33 AM
But it's clear that I might be in the minority here. Anyway, I like var. It's shorter to type and has the same significance as the actual type does.
#13
Re: To 'var' or not to 'var'?
Posted 13 December 2011 - 12:50 PM
But I don't think it's good to use:
var i = 4; //OR var s = "Hello World!";
Just for readability's sake...
But I guess it's just style so it's not to be argued about!
#14
Re: To 'var' or not to 'var'?
Posted 06 February 2012 - 06:37 PM
RudiVisser, on 13 December 2011 - 08:45 AM, said:
I'm still using var in LINQ, but I've also started using it when I simply can't be bothered typing out full names like ThisIsAReallyLongClassName longClass = new ThisIsAReallyLongClassName();.
The problem with this is that it feels like mixing code conventions when you see something like this, for example:
Socket mySocket = new Socket(); var longClass = new ThisIsAReallyLongClassName(); IPAddress myIP = new IPAddress();
9 times out of 10, I find I only have to type 'ThisIs', then just hit the tab key and let VS autocomplete it for me. In some cases, I may have to include AR, or hit the up or down arrow once or twice, but I rarely have to type more than half the class name
VS is even capable of contextually determining the exact class for you, if your explicitly declare your variables. When you use var longClass, VS doesn't really know what longClass is supposed to be, so you have to explicitly tell it. if you use ThisIsAReallyLongClassName longClass, then after you type '= new', as soon as you hit the spacebar, VS instantly selects the ThisIsAReallyLongClassName in the autocomplete list (because you've already told it the exact type of longClass), and you're a single tab-stroke away from entering it.
Visual Studio's auto-complete capability is pretty extensive, and if you utilize it, I don't see var saving you m/any keystrokes.
#15
Re: To 'var' or not to 'var'?
Posted 07 February 2012 - 06:02 AM
jRaskell, on 06 February 2012 - 09:37 PM, said:
Try this example. First, you have a method that returns a Dictionary<int, string>.
Dictionary<int, string> myDic = GetMyDictionary();
or
var myDic = GetMyDictionary();
I used IntelliSense and it took by 27 keystrokes to type the first example. Second example took 19 keystrokes.
So sure, it's not saving anything when you are declaring a bunch of int variables, but it does save strokes in some situations.
|
|

New Topic/Question
Reply



MultiQuote







|