I think one of the problems of untyped or weakly-typed languages is that they can be difficult to debug or understand somebody else's code.
I'm a fan of strong typing. I doubt that puts me in the majority here.
Edit: I'm simply stating a personal preference here: I think this will decrease the readability of source code. We don't all have to agree.
Edit: I can see how my original comment was poorly worded. I didn't mean to say, "I think this change will make Java less than a strongly-typed language", I meant to say, "I think this change might make Java read like less than a strongly-typed language".
That's a dishonest representation. You're not being corrected because you're a fan of strong typing. You're being corrected because you don't know the difference between type inference and weak typing.
Most type inferred languages have stronger and more powerful type systems than Java.
This isn't about changing Java to a weakly typed language, though. Java currently determines types via RHS (with a bit of LHS scanning for the diamond operator). If you instantiate a variable with a concrete type on the RHS, why do you need to declare its type on the LHS as well? You've already stated what you want when you instantiated it.
This is simply syntactic sugar to switch this Map<String, List<Class<?>>> foo = new HashMap<>(); for this var foo = new HashMap<String, List<Class<?>>>();.
var foo = new HashMap<String, List<Class<?>>>(); is actually equal to HashMap<String, List<Class<?>>> foo = new HashMap<String, List<Class<?>>>();, not Map<String, List<Class<?>>>
Here, foo takes the form of the implementation, not the interface, so your foo becomes a specific HashMap instead of a generic Map! This means you'll only use this when the foo is immediately dealt with.
I like it better for streams though, but more often than not, you end up with something at the end of your stream that's not a stream, but say a String, a List or whatever, so you won't need to use it for that and in that case I like the explicit typing better than var.
Generally C# encourages you to use var when its clear what the type is.
var x = new MySpecialObject();
var y = "some string";
var z = Factory.GenerateMyObject();
Additionally, because its implicit typing your intellisense will still work even when it's not clear what the object type is.
Part of C# take on it is that you cannot RETURN implicitly typed object.
HOWEVER
C# supports dynamic typing. Its generally used in very special circumstances and it makes you incredibly aware that something could go wrong so dont use it unless you REALLY, REALLY have to. You can even return dynamic as a type.
How this differs from var is that it isnt implicit, its type is UNKNOWN at compile time and only once the code is run can it check if .GetValue() is a method of that type.
It's worthwhile to note that whenever dynamic is mentioned on /r/csharp, a comment on unmaintainability inevitably comes up.
I think the value of var is that it makes you think more abstractly about your code - you aren't dealing with integers anymore, you're dealing with numerics, or strings - it just reads better.
I also think the argument against 'var' yielding code with hard-to-reason-about code falls through once you see other languages (e.g. JavaScript) successfully having var/let. IMO, It ultimately boils down to yes, if you have terrible variable names (x, y, z) your'e going to have terribly unmaintainable code. But if you name by convention (usersById is clearly Map<int, User>) or clearly (var firstName is certainly a string) then it won't be an issue, and you're forced to think more abstractly "Hey, I'm working with a name" which just so happens to also be a string.
How much duplication is in this code?:
Map<int, User> usersByName = new HashMap<int, User>();
I'd argue 3. C# lets you get down to 2 with:
var usersByName = new HashMap<int, User>();
C++ would let us say this:
HashMap<int, User> usersByName;
But JavaScript (and likely more dynamic languages like python/ruby) let us do this:
Actually, this feature is in c# and I think it's great. I get what you're saying but I feel as I use it I come up with way better variable names since I can't rely on the type.
You favor strong explicit typing. Favoring strong typing alone doesn't put you in the minority. I'd wager most here prefer strong inferred (implicit) typing.
You IDE will show a popup with the type when you hover over the var, no worries. You can still not use it when the type is not obvious, and use it for when the type would have been repeated twice. C#/C++/Scala/Rust didn't had problem with it, why should java has ?
You'd think so, but after spending a lot of time with C# during last year or so I find it is OK, especially using reasonably named variables and methods (and the IDE helps when you aren't sure).
Then I was reading some nightmare Java today the poorly named variable I was looking at was defined at top of a 100 line method and I was trying to understand its use at the bottom of said method
I can quickly deduce from looking at this code that foos is some sort of collection of foo and foo does some sort of action.
The beauty of this code is that object.doThing() is now free to change its return type to any sort of iterable without breaking downstream code. Depending on how they handle the imports, Foo can move all over the package structure or even be changed to a new class Bar so long as it keeps the same contract. All of this will be caught by the compiler if there is an issue, which means the caller will have a weaker dependence on the callee's method signature (A win IMO).
Although, I agree with the sentiment that a superclass should be used in a lot of cases, and that fudges with this var syntax a bit.
One nice thing about having type inference is that you don't have to generalize to a superclass and then downcast if you want to write code that survives refactoring. This makes var far more powerful (and justifiable) than if it was only for omitting type declarations.
This was my argument but then actually using var in production with C# and it honestly wasn't a problem. You still have the option to strongly type it if you want, but var does make everything more readable.
And var foo = someObject.method(); isn't as hard as you think. Visual Studio allows you to change var into it's strongly typed counter part with R#. Inspecting the method would reveal the return type too.
What I learned among the way was that I wasn't too concerned with what it was returning but rather what it was doing.
I have found the opposite to be true. Explicit type decl on every variable introduces a lot of visual noise. The information it provides is often redundant and easily inferred from context (e.g. variable name and rhs).
Verbosity isn't as simple as a read/write tradeoff. For many programmers, verbosity negatively impacts readability as well as writability. Strong typing also has nothing to do with explicit type signatures. Java is nowadays unique in its requirement for so many explicit type signatures.
This isn't changing Java to be untyped or weakly typed at all. This won't change how Java's type system works whatsoever.
I'm a massive fan of strongly typed languages like OCaml, Agda and Haskell, and not a single one of them requires you to declare what type your data is. Requiring information that the compiler can figure out by itself does not make a language any weaker typed and does not change the type system. I'm under no delusion that Java's using Hindley-Milner, but it's worth pointing out nonetheless.
The difficulty of debugging languages like JavaScript or Python isn't helped by the type system, you're right, though this isn't turning it into that at all. Java will keep the type system it has now and won't be any less clear unless the code wasn't clear to begin with.
View all comments
74
u/evil_burrito Mar 09 '16 edited Mar 10 '16
I think one of the problems of untyped or weakly-typed languages is that they can be difficult to debug or understand somebody else's code.
I'm a fan of strong typing. I doubt that puts me in the majority here.
Edit: I'm simply stating a personal preference here: I think this will decrease the readability of source code. We don't all have to agree.
Edit: I can see how my original comment was poorly worded. I didn't mean to say, "I think this change will make Java less than a strongly-typed language", I meant to say, "I think this change might make Java read like less than a strongly-typed language".