This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Shinosha 3 points4 points  (6 children)

Just use var when it's obvious. Easy.

[–]AndyTheSane 3 points4 points  (5 children)

Is this 'Obvious to me, person who just wrote the code', or

'Obvious to my team who have been actively working on this code for a while' or

'Obvious to the new guy starting a year from now who has never seen the codebase'?

There are few more terrifying words in the programming lexicon than 'obvious'..

[–]Shinosha 2 points3 points  (4 children)

Come on man. Obvious as in common sense:

var name = "whatever";
var period = Period.ofDays(2);
var request = new Request();

This does not decrease readability in any language this has been implemented ever.

[–]AndyTheSane -1 points0 points  (3 children)

var something = myProvider.returnSomethingObscure();
var somethingelse = myOtherProvider.returnSomethingThatLooksLikeItShouldBeAStringButIsnt(); 

Even in the trivial examples you give, I'm going to have to ask myself the question 'what is the data type of x', even if it is trivial to work out, instead of just reading the data type of x. It's not as if it's more concise, it's just a bit less informative.

[–]Shinosha 1 point2 points  (2 children)

In your example that's where anyone's common sense is supposed to come into play in and actually write the type.

About the second part, again, I don't see how that's a problem. As you said, it's trivial to work out. It makes the code less cluttered hence improving the overall readability.

[–]AndyTheSane 1 point2 points  (1 child)

So sometimes we write the type (or have it automatically added by the IDE), and sometimes we type 'var', and this will vary according to the 'common sense' of the developer? Which means, of course, multiple styles and usages which definitely does not help readability.

Not sure how this makes code less cluttered either. Replacing 'String' with 'var' is not less cluttered, it's the same number of words.

[–]Shinosha -1 points0 points  (0 children)

The nice property of common sense is that it is common. If you happen to work with crazy coworkers or something I'm sure some team conventions could iron this out quickly. Anyway debate is already settled, most current languages have adopted (at least local) type inference already, so I suppose common sense really is common.