To 'var' or not to 'var'?

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

45 Replies - 27233 Views - Last Post: 20 April 2015 - 06:43 AM

#31 marty617  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 33
  • Joined: 14-October 12

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

Posted 27 October 2012 - 07:38 AM

My OCD opinion is to always be as explicit as possible while understanding that sometimes you cannot be. HOWEVER, keep in mind that using var because you "have to" might be 'code smell' because you do not follow the advice in the first sentence elsewhere.
Was This Post Helpful? 0
  • +
  • -

#32 Curtis Rutland  Icon User is offline

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


Reputation: 4739
  • View blog
  • Posts: 8,437
  • Joined: 08-June 10

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

Posted 29 October 2012 - 06:41 AM

Again, I don't think this fetish for the explicit is necessary. If we all were working with older tools, maybe. But with all the capability VS gives us to inspect static code, I believe we should spend our time writing efficient, clean, easily-refactorable, and non-redundant code. Var is a good option in that direction.
Was This Post Helpful? 1
  • +
  • -

#33 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2539
  • View blog
  • Posts: 4,641
  • Joined: 24-September 10

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

Posted 05 November 2012 - 01:50 PM

var when it doesn't break readability.

That's how I work.

I can see Curtis's expectation of an IDE, but I personally consider readability to be readable with out the IDE.

But see, that's just preference... and really, are we going to pull out the swords over that?
Was This Post Helpful? 1
  • +
  • -

#34 pokiaka  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 76
  • Joined: 05-August 11

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

Posted 14 November 2012 - 06:46 PM

I'm not sure if this is ideal, but I'm using var a lot in-order to write less (and so I'm much more productive.. it has nothing to do with me being lazy.. uhh.. nope.. not at all), and also (and that's justified!) to repeat as less as possible. It's easier to change when you've got to change only once at the declaration, not to mention it's much more readable.

I won't use it in cases where I'm not explicitly allocating a new:

var i = 23m;

(Notice that the variable i is actually a decimal.

I also won't use it in cases where I never explicitly write the type I want. Random is a wonderful example:

Random r = new Random();
var x = r.Next();

This post has been edited by pokiaka: 14 November 2012 - 06:48 PM

Was This Post Helpful? 0
  • +
  • -

#35 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 29
  • View blog
  • Posts: 359
  • Joined: 27-April 08

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

Posted 01 November 2013 - 10:01 AM

View Postlordofduct, on 05 November 2012 - 09:50 PM, said:

var when it doesn't break readability.

That's how I work.


Pretty much this.

If var makes the code difficult to understand I won't use it. But, I'm also a believer that naming conventions should produce names that are descriptive enough for you to infer it's type from the name.

The names of the variables, methods etc should make the code readable not necessarily the way you declare them.

This post has been edited by danny_kay1710: 01 November 2013 - 10:02 AM

Was This Post Helpful? 0
  • +
  • -

#36 nekopczynski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-February 10

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

Posted 05 March 2014 - 08:56 AM

The usage of Var is only "Required" when using anonymous types.
var v = new { Amount = 108, Message = "Hello" }; 


Other than this usage it is entirely up to the developer and personal preference as to when or when not to use the var keyword.
Was This Post Helpful? 0
  • +
  • -

#37 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 4089
  • View blog
  • Posts: 13,051
  • Joined: 05-May 12

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

Posted 09 March 2014 - 03:00 PM

... and whether or not you use Resharper with its default settings.
Was This Post Helpful? 1
  • +
  • -

#38 Curtis Rutland  Icon User is offline

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


Reputation: 4739
  • View blog
  • Posts: 8,437
  • Joined: 08-June 10

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

Posted 10 March 2014 - 07:11 AM

That's where I learned my preference for it. But you can tell Resharper to stop complaining about that if you don't like it.
Was This Post Helpful? 0
  • +
  • -

#39 BobRodes  Icon User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 588
  • View blog
  • Posts: 3,036
  • Joined: 19-May 09

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

Posted 09 January 2015 - 08:32 PM

I'm late to this party for sure.

I generally use it like this:
//Instantiating a new class: this
var t = new Thread[3];
//Looks better than this
Thread[] t = new Thread[3];
//I prefer to avoid it when the type is the result of a method call
string json = new JavascriptSerializer().Serialize(exportList);
//not this
 var json = new JavascriptSerializer().Serialize(exportList);
//Also I like this
var s = new int[] { 2, 3, 4 };
//better than this
int[] z = new[] { 2, 3, 4 };
//just because it seems more intuitive.
Other than that, I pretty much only use it when it's required. I guess I can say I use it when I'm instantiating a class and would have to use the class name twice over, like ClassName myObj = new ClassName().

This post has been edited by BobRodes: 09 January 2015 - 08:35 PM

Was This Post Helpful? 0
  • +
  • -

#40 Curtis Rutland  Icon User is offline

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


Reputation: 4739
  • View blog
  • Posts: 8,437
  • Joined: 08-June 10

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

Posted 09 January 2015 - 09:58 PM

It's been a while since I've thought about this. Still a big proponent of the var keyword. Makes refactoring significantly easier and removes redundancy. Also improves readability in my opinion. It can obscure a bit of information, but I heavily rely on my tools that make that no problem at all. VS makes discovering the type of any implicit variable quite easy.
Was This Post Helpful? 0
  • +
  • -

#41 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 6237
  • View blog
  • Posts: 13,372
  • Joined: 16-October 07

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

Posted 10 January 2015 - 07:50 AM

Many moons ago, I responded to a different thread on this subject. I was rather cool to the var.

Time has passed. I've accepted var and use it every chance I get. Indeed, only really resorting to type declaration when I have to. I once heard a Microsoft dude say, when using var, "I prefer all the action on the right side of my declaration: deal."

I use var so much now that when I don't use it it becomes a flag. Why be explicit here? What special thing is going on? In this respect alone I find it quite helpful.
Was This Post Helpful? 0
  • +
  • -

#42 gonzaw  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 45
  • Joined: 18-December 12

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

Posted 21 February 2015 - 09:48 PM

The "var" keyword allows the compiler to infer the type of your variable. Like any other language with type inference, there is indeed a discussion on whether you should be explicit of your type or let the compiler infer it.

There are basically 2 scenarios:
  • The compiler doesn't have enough information to infer the type: I'm not sure if this can be the case with C# and "var" though. In other languages this can happen. In this case there is no other way but to write the type explicitly
  • The compiler can infer the type: Here you do have a choice of using "var" or not using it.
    • Let the compiler infer the type: This gives you some benefits. You don't have the cognitive load of trying to figure out the type by yourself (specially if it's complicated, like lots of nested types). It also saves you keystrokes, if the name of the type is long (or if you need to qualify it with a namespace for example). If you don't really know the type, and you want the most possible general type, you can use "var" to let the compiler find it for you. This can work if you have a lot of covariance/contravariance/subtyping in the right side of the assignment, and can't really follow all those rules mentally yourself.
      Another benefit is that if you refactor your code, and the type of said variable changes, you don't need to actually modify your code, because the new type gets inferred by "var" too.
      Plus, I believe Visual Studio's Intellisense can actually tell you the type if you hover over the variable and stuff.
    • Write the type explicitly: You should write down the type explicitly if you want to carefully document your code. Types are a form of documentation. The more expressive your type system, the more they can document. This means that, for people reading your code in the future, they will know the types of each individual intermediate variable, and thus will be able to understand your code better, specially if your variable names aren't descriptive enough.
      Another benefit is that if you refactor your code, and the type of said variable changes, the compiler will tell you all the places it changed, because you explicitly typed the assignment. This tells you all the places where there is a type error, and you can go back and see if it's a new error in the logic of the code, or if you just need to change the type of the variable


In the end, the choice is yours. There is no clear answer. Do you value documentation or convenience more?
Notice how I mentioned refactoring on both situations above. If the type of your variable changes, there are both benefits and drawbacks in having either an explicit type, or using "var". Depends on what you want.

This post has been edited by gonzaw: 21 February 2015 - 09:56 PM

Was This Post Helpful? 0
  • +
  • -

#43 sholle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-November 09

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

Posted 18 April 2015 - 10:50 AM

View PostAdamSpeight2008, on 29 March 2012 - 05:52 AM, said:

The default is double, Decimal has different constant format specifier M


I'm assuming here that you know all the default types for any assignment and you expect anyone reviewing your code to know the same?

View Postdanny_kay1710, on 01 November 2013 - 11:01 AM, said:

View Postlordofduct, on 05 November 2012 - 09:50 PM, said:

var when it doesn't break readability.

That's how I work.


Pretty much this.

If var makes the code difficult to understand I won't use it. But, I'm also a believer that naming conventions should produce names that are descriptive enough for you to infer it's type from the name.

The names of the variables, methods etc should make the code readable not necessarily the way you declare them.

Can I get a quick example of where var IntCount = 10 is simpler than int Count = 10?

View PostCurtis Rutland, on 07 February 2012 - 08:25 AM, said:

And IMO, it reduces redundancy. Why do you need to know it's a ClassName twice? Once is fine.

Again, I use it pretty much any time I can, and it works for me.


Only if your are looking for the class definition and aren't in a VS environment.
Was This Post Helpful? 0
  • +
  • -

#44 Curtis Rutland  Icon User is offline

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


Reputation: 4739
  • View blog
  • Posts: 8,437
  • Joined: 08-June 10

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

Posted 20 April 2015 - 06:32 AM

Quote

and aren't in a VS environment


I, and many other .NET professionals I know, are essentially never coding outside the VS environment. I honestly can't think of a reason I would want to do that. Every company I've worked for has had licenses. If they didn't, I'd find a company that did.
Was This Post Helpful? 0
  • +
  • -

#45 Curtis Rutland  Icon User is offline

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


Reputation: 4739
  • View blog
  • Posts: 8,437
  • Joined: 08-June 10

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

Posted 20 April 2015 - 06:38 AM

View Postsholle, on 18 April 2015 - 12:50 PM, said:

View Postdanny_kay1710, on 01 November 2013 - 11:01 AM, said:

View Postlordofduct, on 05 November 2012 - 09:50 PM, said:

var when it doesn't break readability.

That's how I work.


Pretty much this.

If var makes the code difficult to understand I won't use it. But, I'm also a believer that naming conventions should produce names that are descriptive enough for you to infer it's type from the name.

The names of the variables, methods etc should make the code readable not necessarily the way you declare them.

Can I get a quick example of where var IntCount = 10 is simpler than int Count = 10?


Why assume either would be what we'd use? Firstly, it would be count rather than Count, if you're following MS best practices, but that aside, count is clearly an integer, even if you can't see the declaration. If you're using a double/float for counting, you're doing it wrong. If it were a value that needed to be measured in floating point, I'd name it something else, like total.

But I don't believe that names should carry all the information about a variable. Because then you end up with names like i_Count or s_someText. You know, bad, ugly names that we used when we didn't have an IDE providing metadata for us.

Since I'm essentially never not in VS, I can mouseover any variable I care to know more information about.

Therefore, I make my object names carry logical information. So you know what the variable is doing. If you need to know it's type, F12 is bound to "Go To Definition", and mousing over gives you the type.
Was This Post Helpful? 0
  • +
  • -

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