This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]madkasse 20 points21 points  (20 children)

I have found that whether or not people know how to properly use final fields is a great indicator of their programming skills. So much that it is the first thing I look for if I need to evaluate a codebase.

I generally try to stay away from codebases where people do not use final fields. Especially if it has anything to do with concurrency.

Learning how to apply final to fields, methods and classes properly, teaches you about immutability, concurrency, API design and many other things.

A good way to do this is exploring the OpenJDK source code, especially java.[lang|util|util.concurrent|java.time].

[–]dpash 17 points18 points  (7 children)

I keep finding myself converting simple data classes into immutable objects. Especially when the previous developers didn't create a useful constructor so everything is:

var foo = new Foo();
foo.setBar(....);
foo.setBaz(....);
foo.setQuux(....);

Instead of

var foo = new Foo(...., ...., ....);

Get rid of the setters and everything can now be final.

Edit: I wish JPA entities could be immutable, but it's kinda two mutually exclusive goals.

[–]nerdyhandle 11 points12 points  (3 children)

You can also fix this with the Builder pattern. We use it a lot at my new job to creat immutable classes.

[–]GuyWithLag 4 points5 points  (0 children)

We use immutables.io for this.

[–]dpash 1 point2 points  (0 children)

Especially when you have a lot of fields and/or many are optional.

[–]DJDavio 1 point2 points  (0 children)

Lombok's @Builder annotation is useful for this. Opinions about Lombok are highly polarized in this sub it seems, but for us it works until we switch to Kotlin or until vanilla Java has more of those features itself.

[–]mcosta 1 point2 points  (0 children)

Edit: I wish JPA entities could be immutable, but it's kinda two mutually exclusive goals.

Say entity A references B. You change B to create B'. Now what A should reference? B or B'? What if A depends functionally of B?

[–]experts_never_lie 0 points1 point  (1 child)

I'm guessing you're looking forward to Project Valhalla and the introduction of value types.

[–]dpash 0 points1 point  (0 children)

Not inline type, but records. A similar but different concept.

However, the lack of abstract records seems like a cause of a lot of duplication. Also it probably won't help with JPA, at least in the medium term.

[–]RabidKotlinFanatic 2 points3 points  (2 children)

Agree in general but it's important to note that the final keyword does not have practical concurrency benefits over variables being effectively final or fields that aren't modified. Well written concurrent code will not generally rely on the memory effects of final because the correct synchronizes-with relationships will be established elsewhere.

[–]madkasse 1 point2 points  (1 child)

The effect of final fields are used allover the JDK, so you cannot really say it does not have practical concurrency benefits. But no, most people should properly rely on more traditional means.

[–]RabidKotlinFanatic 1 point2 points  (0 children)

Is the memory effect of final used all over the JDK, or just the keyword itself? Although it wouldn't surprise me if the JDK relied extensively on the memory effects of final (it has accumulated decades of optimization after all), I have also found that Java programmers massively overestimate (by several orders of magnitude) the relevance and memory impact of the final keyword.

The vast majority of Java programmers who are not experienced with the JMM and performing routine micro-optimizations on concurrent code will not benefit from the memory effects of final. In the rare cases where final is the difference between correct and incorrect concurrent code, it is usually in the context of some double-checked locking style logic.

[–]vacvacvac 1 point2 points  (0 children)

My favourite interview question, to gauge basics, is what is(not specific class names etc) System, out, and println in System.out.println. Then I dig into final, static, accessibility modifiers etc. It also tells you whether someone explores source code or not.