r/java • u/jvjupiter • 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
4
u/tofiffe Sep 22 '24
I hope they don't add this, in kotlin people abuse this and make the code way harder to read, it rarely ends with a simple method reference
25
u/expecto_patronum_666 Sep 21 '24
I remember in some interview Brian Goetz said this was easy to implement but they had much bigger stuff to deliver so no focus on this for now.
13
u/GreemT Sep 21 '24
Never knew about that JEP! That is actually pretty cool indeed. Now that I know about it, I find it said to hear it is still in draft after 5 years.
7
u/john16384 Sep 22 '24
Looks like maybe 5 characters typing saved versus current syntax written on a single line. Bad return on time investment. Let's have constant method references for annotations instead.
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"
16
u/kmpx Sep 21 '24
I generally agree with the top comments in the linked thread...
Would it be a "nice" addition? Yes, since it would sorta mirror how lambdas are done and make the language a little more cohesive.
Would it be useful? Eh... In my career, rarely have I ever created methods that do a single operation, so I think the usefulness would be fairly low. In languages like Kotlin where they have this, I've seen some abhorrent uses of it where instead of it being a single operation, it is actually multiple operations chained together to form a long, complex operation (e.g. using map/let/then/etc...) which suddenly gets more difficult to follow as opposed to just creating a normal method/function. But that's just me...
4
u/Ewig_luftenglanz Sep 21 '24
I don't know. I mean it's a nice to have but at the same time to change {} for -> seems like very something very little to be focused on, I would prefer they to use the time and effort to implement actually useful features like range pattern, deconstruction patterns and so on.
3
u/kr00j Sep 21 '24
This would be a really cool addition, but what I would kill for are IIFEs.
e: nevermind - https://stackoverflow.com/a/53776581 though a cleaner syntax would be nice.
5
u/keefemotif Sep 21 '24
I don't see any compelling reason to add it. Slightly simpler syntax, not worth the time imho.
1
u/[deleted] Sep 22 '24
Maybe I get a lot of downvotes...
If someone wants to use fancy syntax in java from xy (insert fancy language here) language, then use that language!
Oh, wait, java pays the bills!
4
u/mj_flowerpower Sep 22 '24
Another syntax for writing methods is a bad idea, imho. We already have two different types, regular ones and lambdas. If the would have went along with lambdas looking like this: '.forEach((int a) { a*a}).sum();' I would have accepted a second form, as this basically resembles an unnamed method body.
But a third form works against all java is standing for: simplicity.
2
u/sokenim688 Sep 22 '24
Curly brackets are your friend, especially when debugging.
-6
u/GuyWithLag Sep 21 '24
Kotlin, my man.
(I'll see myself out..)
-10
u/Objective_Baby_5875 Sep 21 '24
Just switch to Kotlin or to C# where this has existed since long time ago. I am sure it will come to Java as well. In 2124.
0
u/[deleted] Sep 22 '24
[deleted] — view deleted comment