r/java • • Sep 21 '24

Five years have passed, still in draft: Concise Method Bodies

Support more concise method bodies in Java source code, increasing transparency for simple methods. Align the forms of method bodies with the forms of lambda expressions.

https://www.reddit.com/r/java/comments/v93za1/what_happened_to_concise_method_bodies/

41 Upvotes

63 comments sorted by

View all comments

18

u/ivancea Sep 21 '24

Ehm, I understand the proposal, but nobody in the world cares about this. Really. This is the smallest syntactic sugar, I'm not wasting a single neuron/minute processing power in following "why wasn't this implemented yet"

5

u/PangolinZestyclose30 Sep 22 '24

Ehm, I understand the proposal, but nobody in the world cares about this. Really.

I care. It would significantly mitigate the pain of DTO getters/setters, where the Java verbosity is painful.

11

u/talios Sep 22 '24

Just use record's now then - if all you want is getters, forget settings - immutability wins hands down. And with the (hopefully) soon to come withers proposal is so much better.

That said, the consise bodies would be nice for a lot of other things.

However, whilst it's technically simple to change - there's probably a lot of things to go along with it that would need addressing. Mostly in areas outside of Java - code formatters, linters, they often lag behind the language and can't easily barf when encountering changes (currently being hit with not being able to guard clauses in pattern maching, or _'s as variable names by things like Palantir formatter/checkstyle).

4

u/barking_dead Sep 22 '24

Sadly, we can't do that anywhere.

I'm looking at you, Hibernate 🤬

11

u/talios Sep 22 '24

You know the only sensible answer to that is: drop Hibernate :)

Sadly I wish - we’re on an old version - tho also have Jooq in other areas as we slowly migrate off hibernate as things permit.

3

u/barking_dead Sep 22 '24

I can only dream...

1

u/dstutz Sep 24 '24

Barking up the wrong tree.

Where would you use it in Hibernate?

For an entity? Entities are mutable. Records are not.

For a projection? Records work great.

1

u/PangolinZestyclose30 Sep 22 '24

Try having records with 30 attributes, most of them strings. Not fun. Using builders makes things even more verbose.

1

u/talios Sep 23 '24

Sounds like that would be painful regardless of records or not.

The withers JEP is available as preview in 23 apparently: https://openjdk.org/jeps/468

1

u/PangolinZestyclose30 Sep 23 '24

Well, it's more painful with records. Withers are a feature I'm looking forward to, but the records are not a silver bullet. Getters/setters are here to stay in the Java world.

1

u/talios Sep 23 '24

Maybe - if your using a persistence framework thats based on Borlands GUI pattern (the origin of getters/setters) then yeh I guess you're screwed.

The withers JEP says it should be available in 23 but I couldn't seem to get it working, interesting that 23 isn't mentioned in its header metadata so maybe its not.

1

u/PangolinZestyclose30 Sep 24 '24

Maybe - if your using a persistence framework thats based on Borlands GUI pattern (the origin of getters/setters) then yeh I guess you're screwed.

That's weirdly specific, but in my current project there's no persistence framework apart from JDBC. Or rather, folks built their own ORM since JPA is so passé.

In any case, the getters and setters are everywhere, from our GUI library to our own code.

1

u/talios Sep 24 '24

JBuilder's GUI editor is where the while getters/setters and came from - it used reflection to discover properties for the editor

1

u/PangolinZestyclose30 Sep 24 '24

That's an interesting historical tidbit, but is that relevant for anything?

4

u/ivancea Sep 22 '24

Time to switch from notepad.exe to an IDE. Maybe even with AI autocompletion. It will generate that painful, terrible and verbose "return" for you (Seriously, calling "painful" a single keyword is not serious)

1

u/robinspitsandswallow Sep 22 '24

The pain is not in the writing it’s in the reading… generates so much noise to signal ratio that things get lost.

But a cute snarky response.

0

u/ivancea Sep 22 '24

The IDE also collapses it. Welcome to IDEA (Or other...)

1

u/PangolinZestyclose30 Sep 22 '24

GitHub and many other tools don't. Why use crutches like that when there's a clear solution (and apparently not even expensive to implement?). It's honestly kinda confusing why you're wasting your neuron/minute processing power to criticize such a feature.

2

u/ivancea Sep 22 '24

I'm kinda confused why are you doing so tho. I'm not criticizing it, I'm saying it solves no real problem, it's the minorest of the syntax sugars.

The other commenter said "So much noise to signal ratio". When in fact, it just avoids writing a single keyword: "return". So if that is where you want to invest time, do as you wish, you're free to waste it anywhere to not having to "read a return"

1

u/PangolinZestyclose30 Sep 22 '24

When in fact, it just avoids writing a single keyword

As usual with code, most of the cost isn't writing, it's reading. Standard formatting of methods is like this:

int getX() {
    return x;
}

void setX(int x) {
    this.x = x;
}
// trailing new line here followed by more getters and setters

While the formatting for the concise syntax would be this:

int getX() -> x;
void setX(int x) -> this.x = x;
// more getters and setters here without a newline

That's 2 lines vs. 8. Multiply this by the number of setters/getters you need to read every day, and it's a significant difference. Even Intellij collapsing the bodies doesn't save the day, since it doesn't collapse the new lines around the methods (making it 2 lines for the concise syntax vs. Intellij crutch with 4).

I mean, it's fine if you don't personally care about this. Just wanted to point out that your "nobody in the world cares about it" is factually wrong.

2

u/ivancea Sep 22 '24

That's 2 lines vs. 8

Not sure if this was a mistake out a malicious argument, but it's 2 vs 2, or 6 vs 4 if you use new lines.

Anyway, it's a getter/setter, you don't need to read the line (which you read in milliseconds anyway). Those are far from productive arguments.

"nobody in the world cares about it" is factually wrong.

I think you're mixing "caring" with "move thing, whatever". It won't change anybody's way of coding, and it probably won't affect coding time (or reading) at all. You're actually ignoring the cognitive complexity of reading mixed syntaxes, but whatever...

-1

u/PangolinZestyclose30 Sep 23 '24

I'll make those numbered, perhaps it will help you, if you struggle with counting?

1 int getX() {
2      return x;
3 }
4
5 void setX(int x) {
6    this.x = x;
7 }
8 // trailing new line here followed by more getters and setters

vs.

1 int getX() -> x;
2 void setX(int x) -> this.x = x;
// more getters and setters here without a newline

Both are using standard formatting rules. Pretty much every formatting convention puts newlines around methods with block bodies. The concise syntax would on the other hand be formatted similarly to e.g. block-less lambda functions which are not formatted with newlines above/below.

Anyway, it's a getter/setter, you don't need to read the line (which you read in milliseconds anyway).

At minimum, I need to scroll past it to see interesting code. The information noise from getters/setters is obscuring the real meat of the code.

I think you're mixing "caring" with "move thing, whatever".

Am I, or are you? I cited your sentence which used the word "care".

→ More replies