To 'var' or not to 'var'?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 24305 Views - Last Post: 10 March 2014 - 07:11 AM

#1 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 782
  • Joined: 27-October 11

To 'var' or not to 'var'?

Posted 13 December 2011 - 04:25 AM

*
POPULAR

So I'm really confused, when should you use the var keyword and when not?

I found this debate but opinions there are as split-minded as they can be?

So your thoughts on the var keyword?
Is This A Good Question/Topic? 7
  • +

Replies To: To 'var' or not to 'var'?

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,469
  • Joined: 29-May 08

Re: To 'var' or not to 'var'?

Posted 13 December 2011 - 05:49 AM

var is essential for working with "mumble" types;- types that can not be directly named by the coder. Try using LINQ without var and you soon get why it was introduced. The type for result of the expression is inferred at compile-time, so the program is still statically typed.

I think the misuse of var is when it used for virtually every variable definition.
Was This Post Helpful? 1
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: To 'var' or not to 'var'?

Posted 13 December 2011 - 05:59 AM

I use var with LINQ(which you kinda have to sometimes), and when doing foreach loops. I find myself giving an actual type to variables though. Sometimes I use it with long datatypes(like generic Dictionaries) just so the declaration statement isn't like 150 characters long.
Was This Post Helpful? 1
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: To 'var' or not to 'var'?

Posted 13 December 2011 - 06:07 AM

same here. i use var only with LINQ or where the declaration is too long and i feel lazy. other then that i use actual type of variables. i kinda find it easier to read it that way :)
Was This Post Helpful? 0
  • +
  • -

#5 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: To 'var' or not to 'var'?

Posted 13 December 2011 - 06:51 AM

Didn't we have a VERY in depth discussion on this a few months ago?
Was This Post Helpful? 1
  • +
  • -

#6 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: To 'var' or not to 'var'?

Posted 13 December 2011 - 07:52 AM

Other than LINQ I don't like using var. I can see its uses in other places but typically don't use it unless I have no other choice, like often is the case with LINQ. As eclipsed4utoo pointed out it can be helpful with foreach loops or very long winded data types. I tend to prefer typing out those long data types as find it makes the intent easier to understand.
Was This Post Helpful? 1
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,861
  • Joined: 02-June 10

Re: To 'var' or not to 'var'?

Posted 13 December 2011 - 08:15 AM

var is useful in more than just linq though. I use it any time I'm not completely sure what the type being returned will be.

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.
Was This Post Helpful? 1
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4498
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: To 'var' or not to 'var'?

Posted 13 December 2011 - 08:33 AM

View PostSergio Tapia, on 13 December 2011 - 07:51 AM, said:

Didn't we have a VERY in depth discussion on this a few months ago?


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.
Was This Post Helpful? 1
  • +
  • -

#9 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: To 'var' or not to 'var'?

Posted 13 December 2011 - 08:45 AM

Since I made that post I've probably changed my mind a little bit.... for the worse.

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

Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4498
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: To 'var' or not to 'var'?

Posted 13 December 2011 - 08:59 AM

I still don't really agree, VS can tell you what the type of anything is.
Was This Post Helpful? 0
  • +
  • -

#11 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 782
  • Joined: 27-October 11

Re: To 'var' or not to 'var'?

Posted 13 December 2011 - 11:22 AM

Thanks for all the feedback! :)

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

Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4498
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: To 'var' or not to 'var'?

Posted 13 December 2011 - 11:33 AM

I don't agree with the last point. If you look at my code, the only time I don't use var is when I absolutely cannot, like a declaration without initialization, or a const declaration.

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.
Was This Post Helpful? 0
  • +
  • -

#13 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 782
  • Joined: 27-October 11

Re: To 'var' or not to 'var'?

Posted 13 December 2011 - 12:50 PM

I learned of it recently, since then I started to worship it!

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! :)
Was This Post Helpful? 0
  • +
  • -

#14 jRaskell  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 06-February 12

Re: To 'var' or not to 'var'?

Posted 06 February 2012 - 06:37 PM

View PostRudiVisser, on 13 December 2011 - 08:45 AM, said:

Since I made that post I've probably changed my mind a little bit.... for the worse.

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.
Was This Post Helpful? 0
  • +
  • -

#15 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: To 'var' or not to 'var'?

Posted 07 February 2012 - 06:02 AM

View PostjRaskell, on 06 February 2012 - 09:37 PM, said:

Visual Studio's auto-complete capability is pretty extensive, and if you utilize it, I don't see var saving you m/any keystrokes.


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.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3