To 'var' or not to 'var'?

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

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

#31 marty617  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • 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: 4437
  • View blog
  • Posts: 7,710
  • 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: 2531
  • View blog
  • Posts: 4,631
  • 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? 0
  • +
  • -

#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: 28
  • View blog
  • Posts: 358
  • 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: 3475
  • View blog
  • Posts: 10,717
  • 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 offline

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


Reputation: 4437
  • View blog
  • Posts: 7,710
  • 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
  • +
  • -

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