To 'var' or not to 'var'?

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

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

#16 Curtis Rutland  Icon User is online

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


Reputation: 4463
  • View blog
  • Posts: 7,777
  • Joined: 08-June 10

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

Posted 07 February 2012 - 07:25 AM

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

#17 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,534
  • Joined: 29-July 11

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

Posted 29 March 2012 - 03:39 AM

If you're using var in instances other than LINQ or when the type is unknown, you're being very sloppy and lazy.

var amt = 2.13;
var num = 3.15;



How would I know by reading the code which is a double and which is a decimal?
Was This Post Helpful? 0
  • +
  • -

#18 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2255
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

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

Posted 29 March 2012 - 04:52 AM

The default is double, Decimal has different constant format specifier M
Was This Post Helpful? 1
  • +
  • -

#19 Curtis Rutland  Icon User is online

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


Reputation: 4463
  • View blog
  • Posts: 7,777
  • Joined: 08-June 10

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

Posted 29 March 2012 - 06:13 AM

You'd know by knowing the language. As Adam says, decimals have an M to identify them.

And that's true whether or not I'm using type inference.

Posted Image

So, don't call me lazy because I don't conform to your particular style guide, especially with an example that's just plain wrong. Personally, I think explicitly specifying a type is redundant, because everything you need to know is on the right hand side of the equals operator.
Was This Post Helpful? 2
  • +
  • -

#20 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2255
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

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

Posted 29 March 2012 - 06:52 AM

The same can be said for most occurrences of the semi-colon and curly-braces.
namespace HelloWorld
  static class Program
    public static void Main()
      var x = 10.5
      var y = 11.5
      Console.ReadKey()


C# looks so much better with F# indenting rules.
Was This Post Helpful? 0
  • +
  • -

#21 Curtis Rutland  Icon User is online

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


Reputation: 4463
  • View blog
  • Posts: 7,777
  • Joined: 08-June 10

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

Posted 29 March 2012 - 07:06 AM

Now that I somewhat disagree with, mostly because I don't like whitespace-significance. Because when someone uses spaces and another person uses tabs, everything can get all wacky. But I could get used to it if it were implemented (which it never will).
Was This Post Helpful? 0
  • +
  • -

#22 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

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

Posted 29 March 2012 - 09:28 PM

I think I'm with you on this Curtis, there's no real reason not to use var. It's statically typed and you can easily tell the data type looking at the right side of the =. Also, if you're using decent names for your variables then the variable name itself should give you a pretty good indication of the type of data that it holds.

of course if you use variable names like
    var i = 8;
    var s = "a string";



then you're likely to have a hard time telling what's in your variables farther down in your code, but that's true whether you define it implicitly or explicitly.

I also don't really see how it's being lazy to use var if, as someone else said, when declaring a variable explicitly you usually only have to type the first three letters and hit enter to get it from intellisense anyway (paraphrased). Either way you're typing about the same amount of characters.

This post has been edited by Nakor: 29 March 2012 - 09:33 PM

Was This Post Helpful? 0
  • +
  • -

#23 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

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

Posted 08 April 2012 - 04:16 AM

View PostNakor, on 29 March 2012 - 09:28 PM, said:

I think I'm with you on this Curtis, there's no real reason not to use var. It's statically typed and you can easily tell the data type looking at the right side of the =.


Not always.
var result = MethodThatReturnsSomething();

Was This Post Helpful? 0
  • +
  • -

#24 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2255
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

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

Posted 08 April 2012 - 04:38 AM

That example demonstrates where the IDE is helpful. Hover over result and it'll tell the inferred type.
Was This Post Helpful? 3
  • +
  • -

#25 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 08 April 2012 - 06:29 AM

I kind of agree with you Adam, in fact I'm on the fence as well; I sometimes use explicit types on methods like that, and sometimes use var.

I don't think I'll ever fully commit to a style. :P (Unless I join a dev team with a strict code guideline)
Was This Post Helpful? 0
  • +
  • -

#26 Curtis Rutland  Icon User is online

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


Reputation: 4463
  • View blog
  • Posts: 7,777
  • Joined: 08-June 10

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

Posted 08 April 2012 - 06:43 PM

View PostAdamSpeight2008, on 08 April 2012 - 06:38 AM, said:

That example demonstrates where the IDE is helpful. Hover over result and it'll tell the inferred type.


That, or F12 on the method, or even hover over the method to see its return type. Frankly, as long as the information is available, I don't care if it takes one second longer to get it, if it maintains a consistent, easily-refactorable style.
Was This Post Helpful? 0
  • +
  • -

#27 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

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

Posted 11 April 2012 - 04:40 AM

View PostMomerath, on 08 April 2012 - 06:16 AM, said:

View PostNakor, on 29 March 2012 - 09:28 PM, said:

I think I'm with you on this Curtis, there's no real reason not to use var. It's statically typed and you can easily tell the data type looking at the right side of the =.


Not always.
var result = MethodThatReturnsSomething();


If you're naming your methods correctly you should still be able to tell what the type is from the name of the method because it's usually going to be something like

var cars = GetVolkswagons();

This post has been edited by Nakor: 11 April 2012 - 04:42 AM

Was This Post Helpful? 2
  • +
  • -

#28 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

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

Posted 27 April 2012 - 02:17 PM

View PostNakor, on 11 April 2012 - 04:40 AM, said:

var cars = GetVolkswagons();

Does that return Volkswagons, Cars, ICars, IVolkswagons, Vehicles, IVehicles, etc.?

As for using the IDE to tell you what type it is, not all code is presented to you inside an IDE.

And Microsoft says

Quote

However, the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required.

Was This Post Helpful? 4
  • +
  • -

#29 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 20 October 2012 - 06:14 AM

My 2 cents ... saving keystrokes is never justification for doing something one way over a more explicit, unambiguous way even if the result is the same for the machine.
Was This Post Helpful? 0
  • +
  • -

#30 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

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

Posted 23 October 2012 - 07:16 AM

View PostMomerath, on 27 April 2012 - 09:17 PM, said:

View PostNakor, on 11 April 2012 - 04:40 AM, said:

var cars = GetVolkswagons();

Does that return Volkswagons, Cars, ICars, IVolkswagons, Vehicles, IVehicles, etc.?

As for using the IDE to tell you what type it is, not all code is presented to you inside an IDE.

And Microsoft says

Quote

However, the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required.


Up vote this post at least 30 more times please :) !
Was This Post Helpful? 0
  • +
  • -

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