To 'var' or not to 'var'?

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

40 Replies - 25548 Views - Last Post: 10 January 2015 - 07:50 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 online

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


Reputation: 4596
  • View blog
  • Posts: 8,065
  • 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: 2538
  • 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 offline

  • Code herder
  • member icon

Reputation: 3740
  • View blog
  • Posts: 11,725
  • 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? 0
  • +
  • -

#38 Curtis Rutland  Icon User is online

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


Reputation: 4596
  • View blog
  • Posts: 8,065
  • 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: 579
  • View blog
  • Posts: 2,995
  • 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 online

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


Reputation: 4596
  • View blog
  • Posts: 8,065
  • 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: 5988
  • View blog
  • Posts: 12,951
  • 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
  • +
  • -

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