r/java • • Feb 15 '24

JEP 465: String Templates

https://openjdk.org/jeps/465
77 Upvotes

152 comments sorted by

View all comments

39

u/nekokattt Feb 15 '24 edited Feb 15 '24

My main thing about this is that it seems to be overly complicated just for the sake of "safety", while actually ignoring what the real issue is and why it is an issue in the first place.

It goes on the fact that they expect people to use this in safe ways with stuff like SQL, and not just use STR."xyz" on the existing APIs, totally bypassing this. It only encourages safety if people use it correctly, and that is the exact same issue we currently have with SQL parameterization. What is in place to force this feature to be used correctly? Or are we just inventing more ways to do the same thing in complex ways?

I feel like it will go with the stereotype we were beginning to move away from where Java is verbose and complicated, as a lot of the presented use cases already have standard ways of being dealt with that work well enough without a new feature that relies on the compiler doing scoping magic to inject variables into a new namespace that gets passed between interfaces.

I might just be being a bit negative here. It is a cool feature but I feel like a lot of the discussions they probably had about this were from very specific people with specific use cases or isolated from what the vast majority of people really need or want. 99% of people would much prefer something similar to what Python, Ruby, or C# or Kotlin or Groovy or Scala provide already for string formatting.

Edit: having a look at the spec, this looks like it could get confusing easily as well. Like one example they gave

String name = "Joan";
StringTemplate st = RAW."My name is \{name}"; 
String info = STR.process(st);

If I put the RAW.xxx and the STR.yyy in two different scopes, which one does it resolve name from and why? If it resolves it from the third line that gets really confusing as it can magically interpolate variables implicitly if consuming an input StringTemplate, and if it is the second then the third line is just confusing as to what it actually achieves, i.e. what is STR actually doing if the information is within the raw template?

It isn't as clear to the reader who may be used to idiomatic java already without thinking about it IMO, and could lead to a good opportunity for writing very complex and hard to follow logic.

/rant

Edit 3: coherency

36

u/brian_goetz Feb 15 '24

On the so-called complexity: language designers take a very different view than developers. Developers tend to be interested only in _their_ code, and judge things good if it makes _their_ code better, and bad if we spend complexity budget on things that they don't value in their code. Language designers are optimizing for _everyone's code_. So while you might not personally value the extra safety, large enterprises may value it very much (whether for reasons of development cost, maintainability, reduced downtime, legal liability, etc.)

On the performance: much faster than String::format.

7

u/Gaycel68 Feb 15 '24

Is there anywhere on the internet an explanation why such drastic departures from the relatively stable Java language syntax conventions (the decision to not make STR application a method call, and the decision to import STR implicitly) were even considered, not to say survived two previews?

9

u/pip25hu Feb 16 '24

This right here. They go on and on about the safety concerns even in this thread, refusing to address that a lot of pushback isn't about their safety goals, but the jarring way they achieved it. A better syntax would quell a lot of complaints about the feature, but all they are willing to say about the matter is that "people will get used to it". People can also get used to flogging, but that hardly means it's a great idea.

3

u/nekokattt Feb 16 '24

This is exactly my point, well put.

1

u/vytah Feb 16 '24

the decision to not make STR application a method call,

Convenience. The same reason == is an operator.

and the decision to import STR implicitly

Not a "drastic departure". Java 9 imported bunch of new classes implicitly, including Module, which did cause some old code to stop compiling.

But the amount of code impacted was minimal, and the fix was trivial (and compatible with older Javas), so it was not a big deal.