News for hackers. Stuff that matters.
Re:Better Java – Resources for Writing Modern Java by meddlepal on Saturday September 19, 2015 @09:24PM (#10246213)
Can you expand on the later point about final please?
Re:Better Java – Resources for Writing Modern Java by mabbo on Saturday September 19, 2015 @10:00PM (#10246297)
tl;dr: final parameters != immutable and safety, and they look like they ought to.

A lot of people write "final" in front of every. single. declaration. And they do that because someone once told them that was good. But then you remember that Java is a language which all non-primitive types are passed by reference, so your 'final' is actually a final reference and not a const immutable object, like you might get in C.

If someone passes in a final FooBar to a method, and you mutate the FooBar during the method for any reason, then after the method is done the object remains mutated.

Just this week, that exact problem caused my team an entire day of headaches. Two methods were being called concurrently using Futures, passing the same object to each method. One method mutated the object slightly, the other relied on it having not been mutated. Hello race condition. I was so proud to have solved it, until I realized I'm the dolt who wrote the bug in the first place.

Silly, I know. But the point is that final makes you feel safe when you might not be.

Re:Better Java – Resources for Writing Modern Java by davnicwil on Saturday September 19, 2015 @10:57PM (#10246422)
> If someone passes in a final FooBar to a method, and you mutate the FooBar during the method for any reason, then after the method is done the object remains mutated.

Fair enough, but if FooBar is your class there's an easy fix for this - make FooBar immutable. If it's not, make an immutable wrapper object for it.

Very true that relying on untrue assumptions about the immutability of your data will cause hard-to-spot bugs, but the solution is then just to guarantee everything is immutable, and define that as an assumption that cannot be false if everyone on the team follows the pattern.

If somebody does break the pattern, then that, rather than the race condition down the line, is the real bug. The dev who wrote the mutable data class can just have that (re-)explained to them, so it doesn't happen again.

Re:Better Java – Resources for Writing Modern Java by lmm on Sunday September 20, 2015 @03:35AM (#10246846)
C const doesn't mean immutable either, and I suspect most Java programmers have no exposure to it in any case.

Honestly I don't think final ever makes things worse. I struggle to imagine someone who would misunderstand in the way you describe having any success in Java - references are a pretty fundamental part of the language.

Re:Better Java – Resources for Writing Modern Java by chii on Saturday September 19, 2015 @11:44PM (#10246498)
i wish java had designed final to be semantically const just like C++ (but without the type level breakages that seems to happen in C++).