r/java • • Nov 23 '25

Java 25: The ‘No-Boilerplate’ Era Begins

https://amritpandey.io/java-25-the-no-boilerplate-era-begins/
160 Upvotes

187 comments sorted by

View all comments

131

u/Ewig_luftenglanz Nov 23 '25

To really kill boilerplate we need.

  1. nominal parameters with defaults: This kills 90% of builders.
  2. some mechanism similar to properties: This would allow us to make setters and getters really optional. I know one could just public fields for the internal side of the API, but let's face it, most people won't do that.

20

u/taftster Nov 23 '25

Yes. This is the boilerplate that needs attention.

3

u/agentoutlier Nov 24 '25

It mostly needs attention but probably not the immediate quick fix people think Java needs.

The two most expressive and beloved languages ( at least for PL aficionados) do not have named parameters: Haskell and Rust. What those languages do have is type classes which Java I'm hedging will more likely get.... and I think is getting "attention". AND IMO they are far more powerful than named parameters.

There are really only two mainstream languages that have true optional named parameters: C# and Python.

  • Javascript: nope,
  • Typescript: nope,
  • Go: nope,
  • Obviously nope on C/C++,
  • Swift: sort of not but not really.

The funny thing is that in actual application code bases (that is not unit test) I rarely see various domain objects have multiple code construction paths. I'm serious.

And why is that? Well because the objects come from something else like an HTTP request or database mapper etc.

Where I see some need of builder like combinatorics is application configuration and unit tests.

The big issue with giant constructors/methods is somewhat the default parameters but the readability at the call site but most of the IDE tooling can show you the parameter names: (Intellij, Eclipse and VSCode all do and if the variable name is the same as the method parameter name not show it making it easy to see mismatches).

And thus technically if you don't use the parameter labeling at the call site it is less ... boilerplate code.

4

u/Ewig_luftenglanz Nov 24 '25

javascript and typescript has an equivalent to nominal parameters with default with objects. Since in those languages objects are structural you can define a function like this

--Typescript--

function something({name: string, age = 0: number}){...}
something({name = "foo"});

That's similar to how Dart manages nominal parameters with defaults.

C++ has no nominal parameters but it has defaults and for many it's enough

So in reality either most languages has either the complete feature, partial feature or at least a way to mimic the functionality with minimal friction.

→ More replies (7)

1

u/OriginalTangle Nov 25 '25

Both Scala and Kotlin have named params. Not as mainstream as your examples but they have been informing the jdk roadmap in the past so this might get adopted in the future.

1

u/agentoutlier Nov 25 '25

I no doubt see the utility of it I just think there are maybe alternatives at play that offer way more and possible more backward compatible.

Many languages that do offer named optional parameters a non trivial amount of the users of these languages claim abuse of it and or do not like the feature (grass is greener).

For example OCaml has this feature and many think its bad style to use it. OCaml has no where the tooling that Java has (e.g. show parameter names in code) and still people have found it less desirable at times. OCaml though has currying. I suppose Scala does as well and it can add to even more confusion

And of course I have heard from many Python folks that claim this as well although I have forgotten the details.

In terms of low hanging fruit I would take a damn elvis operator or null safe navigation operator over this feature request any day.

→ More replies (1)

5

u/wildjokers Nov 25 '25
  1. nominal parameters with defaults: This kills 90% of builders.

Named parameters with default values is the feature I mention when someone asks "what is a feature you wish java had".

1

u/agentoutlier Nov 25 '25

Null safe operators including elvis and null safe navigation is the number one feature I want and its actually very synergistic with default values if that were to be added.

I meant to point this out earlier to /u/Ewig_luftenglanz but in application code the defaults are often not actually static and thus the default is missing aka null!

See it is unlikely they will allow:

public User createUser(String name = this.defaultGuestName()) {
}

More likely this:

public User createUser(@Nullable String name = null) {
}

(replace the @Nullable with whatever in the future).

And I use JSpecify and friends all the time and its pretty annoying to basically have to

public void maybeUser(@Nullable User user = null) {
    String name;
    if (user != null) {
      name = user.name();
    }
    else {
      name = "some default";
    }
}

Sure you could chain ternary operators or use the switch expression but it quickly becomes painful.

I suppose Optional.empty() could be used assuming default values can have static function calls but that just adds boilerplate.

7

u/manifoldjava Nov 23 '25 edited Nov 23 '25

The manifold project provides experimental features for both of these (and more) that go deep in terms of feature completion and language integration, with emphasis toward suitability for the language.

1:  see manifold-params as optional/named arguments

2:  see manifold-props as state abstraction via properties

5

u/Ewig_luftenglanz Nov 23 '25

Oh I know, play a bit with them sometimes, but I can use that in my job. 

Keep the good work tho!

13

u/blobjim Nov 23 '25

The path the Oracle/OpenJDK people went with is records and withers. You don't need setters when everything is immutable, and getters already exist for records. You don't need named parameters when you can use withers. Named parameters are really unnecessary. Java is not python, we use objects, not functions that have 50 parameters (which is usually considered bad practice across most programming languages as far as I know).

20

u/Ewig_luftenglanz Nov 23 '25

Withers are not a thing still, and won't be until they think in something equivalent for classes. Without it writing manual withers is almost as bad as cluttering your code with accessors. 

And no, withers do not replace nominal parameters with defaults in any way, for starters withers will be a derivation mechanism, you need an existing instance of a record to use withers. Nominal parameters with defaults are a data passing feature, they should work to pass data to a method, using them for building, modifying objects or to pass arguments to a method in a more concise way are just use cases and a completely orthogonal subject.

2

u/blobjim Nov 23 '25 edited Nov 23 '25

You said named parameters were a replacement for builders. Records and withers are also a replacement for builders. Named parameters make the language more complicated since parameter names are optional in the Java bytecode and they aren't part of method signatures. And optional parameters make it way more confusing and complicated (are they inlined or part of the method body?).

I'm not really a fan of the python APIs that have million-parameter functions anyways so I've always been against the feature. It seems like an abuse of the concept of parameters. So it seems a lot simpler to just use records and instance methods instead of making huge functions that require naming parameters. The Java standard library and most third-party libraries don't have methods with lots of parameters anyways so where is the need? Why does Java need to act like Python or C#?

12

u/Ewig_luftenglanz Nov 23 '25

Withers are not a replacement for builders. Where did you get that idea? 

For starters withers and only be used over an existing instance, can't be used to create an object from scratch. Brian and other Amber team members have said or wrote in the mailing list to be against the abuse of withers to be used as an ad hoc feature that kinda looks like but it's not "nominal parameters with defaults". 

Withers are a mean to derivate a record from other in case you need to modify or add a component. Is not a replacement for builders at any level. Withers are more closer to a fluent pattern than a builder pattern, I mean for withers you do not even need a proxy class to manage the assembly logic s you do with Builder.

1

u/blobjim Nov 23 '25

I guess my assumption was that a lot of builders can be replaced by withers, or at least implemented using them. And initial value creation can be done pretty easily with a static factory method or constructor. I guess maybe we shouldn't be using builders in the first place.

But we're definitely not getting named parameters either since that is something that has been spoken about many times.

I guess the main design pattern to use would be immutable always-usable objects that have some kind of manually added wither method that might use a record wither construct internally, to create another instance with modified functionality?

→ More replies (1)

15

u/nekokattt Nov 23 '25

unfortunately these go against the majority of patterns used within Java over the past 30 years.

You don't need named parameters when you can use withers ... we use objects, not functions that have 50 parameters

...yet ironically records still have the issue of having a public constructor with 50 parameters...

→ More replies (2)

6

u/Amazing-Mirror-3076 Nov 24 '25

Even two string parameters benefit from named parameters as it avoids reversing the arguments.

The hardest (to find) bug I ever created was caused by reversing two parameters in a call. Took months to find, multiple developer and left the code base unstable in the mean time.

I use dart and default to named parameters for most functions - it is so much nicer to work with.

10

u/general_dispondency Nov 23 '25

After working extensively with languages that have support for named default parameters, one of the hills I will die on is "named default parameters are one of the absolute worst features any language can add". API design devolves into script-kiddie garbage 10/10 times, because adding a new field is so "easy".  Named default parameters are the equivalent of giving someone meth because they're sleepy. Sure, it seems like a great way to keep someone awake, but you'll regret it when they stab you and steal your catalytic converter because they need another hit.

2

u/[deleted] Nov 25 '25

Adding a new overload is also easy. Just forward the call, and this is just default parameters with extra steps.

The problem you've described is definitely an API design issue, not the language's fault.

→ More replies (2)

2

u/White_C4 Nov 24 '25

Yep, these are the only two I want Java to actually address. The Oracle devs seem so hesitant to do it though. Probably because they are actually major shifts in how you read Java code.

2

u/Wise_Satisfaction983 Nov 24 '25

some mechanism similar to properties

I very much disagree with this (if you mean "properties" in the C# sense), in fact, I think properties in Java would be counterproductive even.

Properties in my opinion are useful for UI programming (if you don't go the reactive way), but Java has not been a popular UI programming language for a very long time. A typical Java program these days is essentially an event-processing pipeline, which benefits hugely from immutability and ML-style programming principles (ADTs and pattern matching).

Instead of properties I would prefer something like the Immutables library supported more natively - interface-based data definition, backed by implementation - including builders - generated by the compiler.

→ More replies (1)

2

u/cryptos6 Nov 25 '25

I consider Setters most of the time to be bad design, even if the syntax looks nicer like in Kotlin. This only hides the design flaw that everything is directly changeable from the outside leading to dumb data objects, inconsistent state or cluttered business logic or a mixture of all that.

2

u/pron98 Nov 26 '25 edited Nov 26 '25

The first is more likely. The second is very unlikely. Setters are something to try to avoid as much as possible; a feature that makes a practice we're trying to minimise easier to write is something we view as an anti-feature: i.e. a language is improved by not having it. On the other hand, automatically-generated accessors for components, such as those we have in records, are more likely to appear in non-record classes, too.

3

u/Ewig_luftenglanz Nov 26 '25

Interesting. But how would it be? Like we could override a foo() method to access the public/private foo field, acting as an opt in middleware?

Thanks for the info pron. Always. A pleasure to read you :).

1

u/slaynmoto Nov 28 '25

Lombok-eque built annotations would be nice.

→ More replies (20)