News for hackers. Stuff that matters.
Re:Var and val in Java? by gravypod on Saturday March 26, 2016 @03:12PM (#11366981)
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.

Re:Var and val in Java? by deanCommie on Saturday March 26, 2016 @03:33PM (#11367063)
If 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.

Re:Var and val in Java? by gravypod on Saturday March 26, 2016 @04:13PM (#11367256)
If 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.
Re:Var and val in Java? by Joeri on Saturday March 26, 2016 @04:46PM (#11367377)
Realistically 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.
Re:Var and val in Java? by gravypod on Saturday March 26, 2016 @05:38PM (#11367579)
What 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.

Re:Var and val in Java? by danbruc on Saturday March 26, 2016 @04:49PM (#11367388)
After 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.
Re:Var and val in Java? by gravypod on Saturday March 26, 2016 @05:37PM (#11367573)
I 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.

Re:Var and val in Java? by danbruc on Saturday March 26, 2016 @06:07PM (#11367674)
Why 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.

Re:Var and val in Java? by gravypod on Saturday March 26, 2016 @06:11PM (#11367688)
>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.

Re:Var and val in Java? by danbruc on Saturday March 26, 2016 @06:29PM (#11367753)
That 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.
Re:Var and val in Java? by whitenoice on Sunday March 27, 2016 @02:09AM (#11369097)
I don't think you have to type it twice even now, if you specify the type on the left, you can just specify <> on the right. So you can write something like - Map<String, List<Xyz>> foo = new TreeMap<>(); This has been present from Java 7. The reason I like this is that when you are parsing through some library code you can just look at the variable declaration and ignore its initialization to figure out what type it is.
Re:Var and val in Java? by bmm6o on Saturday March 26, 2016 @05:30PM (#11367545)
This 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).

Re:Var and val in Java? by gravypod on Saturday March 26, 2016 @05:39PM (#11367581)
If 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.

Re:Var and val in Java? by x0x0 on Saturday March 26, 2016 @03:20PM (#11367018)
Are you seriously arguing we should optimize the java language for people who aren't familiar with the language?
Re:Var and val in Java? by gravypod on Saturday March 26, 2016 @03:25PM (#11367034)
I'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.

Re:Var and val in Java? by deanCommie on Saturday March 26, 2016 @03:33PM (#11367065)
Well, 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.

Re:Var and val in Java? by masklinn on Saturday March 26, 2016 @03:59PM (#11367186)
> 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.
Re:Var and val in Java? by gravypod on Saturday March 26, 2016 @04:13PM (#11367250)
What type are they?
Re:Var and val in Java? by heinrich5991 on Saturday March 26, 2016 @04:20PM (#11367285)
Depends 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&versio...
Re:Var and val in Java? by gravypod on Saturday March 26, 2016 @05:32PM (#11367551)
That's my problem. YOU can't tell. You cannot just look and see what is going on.
Re:Var and val in Java? by masklinn on Saturday March 26, 2016 @06:11PM (#11367689)
Sure 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.
Re:Var and val in Java? by masklinn on Saturday March 26, 2016 @04:33PM (#11367330)
Whatever 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).
Re:Var and val in Java? by pohl on Sunday March 27, 2016 @03:28PM (#11371140)
One wonders how these people were able to learn Java in the first place.
Re:Var and val in Java? by sgift on Saturday March 26, 2016 @05:14PM (#11367480)
No 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).
Re:Var and val in Java? by kuschku on Saturday March 26, 2016 @05:37PM (#11367572)
The issue is more with "reading code from other people written like that" and less with "understanding your own code".
Re:Var and val in Java? by cle on Sunday March 27, 2016 @08:22PM (#11372080)
What are the costs of this?