r/programming • • Mar 09 '16

var keyword proposed for Java

http://openjdk.java.net/jeps/286
250 Upvotes

395 comments sorted by

View all comments

Show parent comments

14

u/Eirenarch Mar 10 '16

Oh yes, it does tell me. It tells me I have an IEnumerable<Customer> rather than IEnumerable<CustomerDto> or PagedList<Customer> or something like this.

2

u/Ravek Mar 11 '16

That should already be obvious from context.

1

u/Eirenarch Mar 11 '16

Well it is not. I know. From real code.

1

u/Ravek Mar 11 '16

Well then why don't you fix the bigger problem rather than slapping an explicit type on it and calling it a day?

1

u/Eirenarch Mar 11 '16

First of all - because the real problem is that sometimes it is unclear simply unclear what the type is, period. In addition if I accept the type is clear but the developers who wrote the code suck then we'll have to fire people and there will be no one to write the code. And finally if the prior argument is true this means that the developers on .NET Core suck and should be fired because strangely they have imposed the same restriction in their code conventions.

1

u/mirhagk Mar 10 '16

IEnumerable<Customer> rather than IEnumerable<CustomerDto>

If the type is telling you whether you are interacting with an API or a database then your code needs to be a lot clearer.

Either it's something you don't care about or you have poorly named variables/functions.

2

u/u551 Mar 10 '16

code needs to be a lot clearer...

Unfortunately we live in a world where a lot of code is mess, and even if you know what YOUR code does, a lot of times it's somebody elses code you need to work on, and knowing types of things with a glance is a blessing imo.

1

u/Eirenarch Mar 10 '16

Well, var isn't telling you anything so you are left at the mercy of my poor naming.

1

u/mirhagk Mar 10 '16

Or having to drag the mouse or cursor all the way over variable name to see the type in the IDE

1

u/Eirenarch Mar 10 '16

Yes but as I already pointed out you simply don't know when you need to drag that mouse cursor. You assumed IQueryable but I was returning IEnumerable. Sucks for you, you just pulled all the data from the database and did paging on the web server.

1

u/mirhagk Mar 10 '16

Well if you returned IEnumerable there's nothing I could do to prevent paging on the web server (short of using some duplicated API method or doing a very dangerous cast to IQueryable

1

u/Eirenarch Mar 10 '16

You have access to the code that returns the IEnumerable you can change it.

1

u/mirhagk Mar 10 '16

Also keep in mind that the original author of the consumer knew the type (or should have). So this would help spot a mistake through casual glance that two people made (or hopefully more because hopefully you do code reviews). Doesn't seem like a really great reason to have a rule that states code must remain polluted.

1

u/Eirenarch Mar 10 '16

The code is not polluted it contains valuable information. As I already said I have seen people (admittedly junior) fail for precisely this reason. I also find myself needing to point my cursor at various vars just to make sure the code works as I expect it to work. For me this is enough of a reason to instantiate this rule whenever I am in charge for conventions. BTW I was pleasantly surprised to find that the .NET Core conventions follow the same rule.

-1

u/[deleted] Mar 10 '16 edited Mar 10 '16

IDE tells you that. Documentation/go to definition tells you that. In most cases it's irrelevant anyway (obvious from use when reading or intellisense when working on it)and it's just noise.

And don't even try to argue "what about people who don't use an IDE" - sorry but if your editor doesn't support features mentioned above (and even Vim does) it's not fit for development with these languages - their highest value proposition is powerfull tool integration.

3

u/Eirenarch Mar 10 '16

I don't want to have to move the mouse over the var keyword or something like that. Also I have tried that empirically with interns. While it is true that interns are far worse than experienced programmers I have seen them make a bunch of mistakes from not knowing the type of the variable under var (most often IEnumerable vs IQueryable and examples like the one I gave). They wrote the code and a week later I ask them to tell me the type of the variable without pointing with the cursor and they fail. The real problem is they don't even know they have to look it up otherwise there would be no bug to begin with.

2

u/Unmitigated_Smut Mar 10 '16

I've seen non-interns make the same mistakes. Types should not be hidden from view. I use an IDE as my crutch when doing spaghetti maintenance, but I'd much rather be able to kick the crutch away.

1

u/Eirenarch Mar 10 '16

I don't claim that only interns do this I just point out that I have empirical observation. Even if only the most junior programmers are confused by hiding types it is still worth to leave the types there.

1

u/[deleted] Mar 10 '16 edited Mar 10 '16

The real problem is they don't even know they have to look it up otherwise there would be no bug to begin with.

Do you think typing it out really helps here ? I guess because they have to type out IQueryable and then they figure out they don't understand what that type is and won't just treat it like IEnumerable ? I can see the value in that.

But in general I think more of my code falls in the above category of "Foo foo = bar.Foo;" and typing out Foo is just pointless verbosity even when it's a result of a method call/getter

1

u/Eirenarch Mar 10 '16

It is not about typing it is about reading the code. While many of the types are obvious you simply don't know when the type is different from what you expect. Maybe that Foo is actually FooBar but you just assumed it is Foo because similar code elsewhere has the type of Foo. BTW in practice while prototyping the code I just go var everywhere and then when I am happy with the design I go back and add the non-obvious types.

1

u/[deleted] Mar 10 '16

BTW in practice while prototyping the code I just go var everywhere and then when I am happy with the design I go back and add the non-obvious types.

I agree with that - I just don't like the rule of "use var only where the type is explicit on the other side" - most of the times I can tell what it is implicitly like the above and more importantly I don't care (if it quacks like a duck) - but there are cases where it's not obvious and when I'm cleaning up the initial implementation I type those out explicitly where I feel it's important.

1

u/Eirenarch Mar 10 '16

While your code may be fine your approach does not work well as a rule. It is far better to simply say that the type should be mentioned either on the right side or the left rather than "write the type when there can be a confusion"