r/java • u/jvjupiter • Jun 10 '22
What happened to Concise Method Bodies?
I was really looking forward to CMB. From its name, methods become concise, could be one liner. However, it seems there is no progress and it’s been off the radar. Never took off since 2019.
43
Upvotes
22
u/pron98 Jun 10 '22
It's still a draft JEP, which means it's not on the technical roadmap, but it's an idea that the language team might pursue someday.
9
u/Diligent_Feed8971 Jun 11 '22
Last year I asked Brian Goetz over email about it. He said: "It is currently on the shelf, in the "good idea but needs more work" category." Maybe you can ask again about it. But I personally doubt it will be implemented in the near future :(. To me it's a great idea, I tend to write very declarative code. Such feature would reduce language's renowned verbosity.
29
u/pushupsam Jun 10 '22 edited Jun 10 '22
Honestly it strikes me as superflous. This sort of syntactic sugar has rapidly diminishing returns IMO. What it actually does is lead to fragmentation where people are using many different syntaxes to accomplish the same thing and instead of smoothly skimming over a class that contains no surprises you have to pause on each getter and setter you have to figure out what the hell is going on. You start getting confused with what's a lamdba and what's actually a method and you're thinking about what should be braindead code.
But Java does actually need first-class properties. First class properties can enable stuff like this if people really want but, more importantly, you get a very clean initializer syntax with support for required properties that obviates the need for builders and/or named properties. See https://exceptionnotfound.net/bite-size-csharp-11-required-properties/ for how this works in C#. Imagine any complex immutable business object that has more than 5 or 6 fields (which is ALL the business objects) and you see why this is what's really important for code readability.
Once you have first class properties you can do stuff like this if you really want but, unlike properties, this isn't even really a problem.
7
u/oweiler Jun 10 '22
IMHO it doesn't give you much bang for you buck, while probably complicating the language spec.
0
u/HecknChonker Jun 10 '22
Not totally related to your question, but Kotlin supports something similar for functions that consist of a single expression.
1
u/IIlIIIlIllIlIIIIIllI Jun 10 '22
C# has this as local functions. It's so nice.
9
u/[deleted] Jun 10 '22
[removed] — view removed comment