3 ms·
I really don't like var/val definition. My issue is that it makes it nearly impossible for someone who is not familiar to the language or to the code base to ju
by gravypod 11y ago
I really don't like var/val definition. My issue is that it makes it nearly impossible for someone who is not familiar to the language or to the code base to jump in and start fixing.
Could someone please tell me the following: How do I tell the type of x?
var x = callSomeRandomFunction()
We have to go look in callSomeRandomFunction, don't we? Now how easy will it be to do that if you have 15 variables and calls to different functions?
How do you know what types are iterable and what types aren't?
What I love about the way explicit definition works is that you are almost guaranteed that in a function you have the type that you are looking for.
If they kill that, they kill one of the greatest parts of Java: the parts that make it not only easy to write but easy to understand.
- x0x0 11y agoAre you seriously arguing we should optimize the java language for people who aren't familiar with the language?
- gravypod 11y agoI'm saying that a language should be easy to understand by anyone. Hell, I'd have a hard time debugging new code if I saw var x, y, z; Var means nothing! It has no implicit meaning. It just means whoever wrote the code was too lazy to write out the type names. If you agree with that or not, just know that the few seconds it would have taken you to type out a full type name will probably save someone debugging your code in the future hours and make it infinity easier for someone who comes along and needs to start working on your code base.
- deanCommie 11y agoWell, lucky for you "var x, y, z" is never going to happen in a statically typed language. dynamic, on the other hand....<looks shiftily into C#'s direction> J/K. I Love dynamics.
- masklinn 11y ago> Well, lucky for you "var x, y, z" is never going to happen in a statically typed language. Why not? let (a, b, c); is perfectly valid Rust, it just declares 3 locals a, b and c.
- gravypod 11y agoWhat type are they?
- heinrich5991 11y agoDepends on what you assign to these variables. fn main() { let (a, b, c); a = true; b = [1]; c = "foobar"; } https://play.rust-lang.org/?gist=ebedd0cd5b6707410aff&version=stable https://play.rust-lang.org/?gist=ebedd0cd5b6707410aff&versio...
- gravypod 11y agoThat's my problem. YOU can't tell. You cannot just look and see what is going on.
- masklinn 11y agoSure you can, it's pretty clear that a is a boolean, b is an array on the stack and c is a string. If the code is so complex you can't eyeball a local scope you probably have bigger problems than whatever the type of the variables are, and if you get one wrong the compiler will yell at you.
- masklinn 11y agoWhatever type the compiler infers based on the assignments you make afterwards (and a compilation error if there isn't any/enough information to infer the type).
- pohl 11y agoOne wonders how these people were able to learn Java in the first place.
- deanCommie 11y agoIf var x = "..." makes it difficult to tell what type x is, the problem is first and foremost in whatever "..." is. If it's your code, FIX IT. Rewrite it in such a way that it IS obvious what type x will be. Intermediary variables, helper functions, and good names for both. (just don't make it a comment. Comments are future code liars and you should feel bad if you contribute more code liars to the world of code) If "..." ISN'T in your control, then...don't use "var". If you work with other developers that don't understand the above 2, review their code, influence them, convince them. If you work with so many of such people that you cannot convince them of the value of this, go work somewhere else.
- gravypod 11y agoIf it's your code it should be obvious, but I can't tell you how many times I've looked at code an it hasn't been. You can't just say "fix it" and it will be true.
- Joeri 11y agoRealistically though, almost everyone who writes java professionally uses a big honking IDE to do it, because Java is too verbose to explicitly type out without code completion, and that big honking IDE can easily tell you the type of x without you having to open callSomeRandomFunction.
- gravypod 11y agoWhat about when you have an exception thrown in a library you started using. You open up the library on github or whatever code hosting system they use. You see a bunch of var this var that, and you have no idea how to follow through the code yourself. You have lost a huge debugging technique, using your eyes, for your lack of patients for writing a variable type.
- danbruc 11y agoAfter using var in C# since its inception I personally say this is a non-issue. At worst autocomplete shows the actual type and the available members and usually you just know the types after a few minutes even if you are looking into some unknown code.
- gravypod 11y agoI don't see how without an IDE you could do that. Something doesn't just stop being a problem because an IDE helps you avoid it. The explicit nature of Java is what makes it great. What you type is what happens.
- danbruc 11y agoWhy would you write code without an IDE? And using var doesn't suddenly make things happen randomly, everything is still perfectly well defined, just less verbose. And it is a feature that 99.9 % of the time does what you want, situations in which you want a different type than the inferred one are rare in practice. Actually the only one that I can remember is initializing a variable with some concrete implementation of an interface and desiring the interface type instead of the concrete type. And this one occurs pretty rarely because I think aiming for single assignment is a good thing.
- gravypod 11y ago>And it is a feature that 99.9 % of the time does what you want While typing a variable name explicitly is a feature that 100% of the time does what you want.
- danbruc 11y agoThat is why you have the choice to be explicit. You choose between shorter and less redundant code and full control over the type of the variable. What is the point of typing out the same type in a single line twice? HashMap<String, AtomicReference<Foo>> fooMap = new HashMap<String, AtomicReference<Foo>>(); What is the disadvantage of using var? var fooMap = new HashMap<String, AtomicReference<Foo>>(); It's less redundant, easier to read, does not clutter the code and has exactly the same information content. And if you really want the variable to be of type Map<,> nobody stops you.
- sgift 11y agoNo one is forced to use the new syntax. If you cannot deduce from the function name what the type is don't use var/val (and think hard about that name .. it is probably bad).
- kuschku 11y agoThe issue is more with "reading code from other people written like that" and less with "understanding your own code".
- bmm6o 11y agoThis is an optional feature we're talking about, isn't it? You won't be forced to use it. It's been in C# for a few versions, and you do find there are places where it always makes sense to use (foreach loops), and there are places where you want to use some judgement. Of course all variables still have a type, it's just not made explicit in the source code, so you can get your IDE to tell you what it is (e.g. by hovering).
- gravypod 11y agoIf it is made available it will be used. That's kind of a golden rule: build it and they will come. If this is widely adopted, I'd say that there will be a lot of problems that stem from it. That's why I say this shouldn't be a feature.
- cle 11y agoWhat are the costs of this?