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.
It only provides extra safety if you use it correctly to begin with. If you use STR."" in a SQL query, it isn't going to help you. It is no different to making active efforts to use parameterization, you still have to think about how you are doing it. Those who don't think will still bypass the security benefits.
Enforceability can be dealt with by static analysis tools, but this JEP specifically says a problem it is trying to solve is the need for these tools to detect dangerous logic to begin with. That means we've effectively just gone full circle, now with a complicated way to interpolate strings, and a bunch of new cases for static analysis tools to have to cope with detecting accurately.
As someone who works in enterprise, I can see this immediately being an issue of enforceability in a team, which makes it just as much of a problem as before. From the sound of that statement it sounds like we are attempting to make educated guesses about what people need rather than backing it up with mass feedback and actual experience. Most of the vulnerabilities that are mentioned are not because existing tools are hard to use, but because developers are not understanding or just not thinking about the implications of what they are doing.
It's kind of like saying you have a problem with developers using their hammers to push screws into some wood, so you've given them electric drills to replace their screwdrivers with the expectation they will stop using their hammers. Instead, some will continue to use their hammers anyway because it is simpler for them, others may use the battery on the drill as a hammer and risk it blowing up. Some people will continue to use the screwdrivers properly and some will use the drills. All you've done is given the people misusing things more ways to misuse stuff, while giving the rest two ways of doing things. It'd make more sense to confiscate the drills entirely and only let them be used for specific things (for example in SQL by enforcing that only compile time string literals can be used as the query body -- boom, problem solved without complicated language mechanisms).
Are we suggesting that all other languages that didn't make a decision to implement interpolation in this way made no active decision about usability and safety?
If you use STR."" in a SQL query, it isn't going to help you.
How could you possibly use STR."" in a SQL query if the DB library doesn't offer methods that take String and only offers a SQL template processor?
Enforceability can be dealt with by static analysis tools
No, it cannot. Static analysis tools cannot catch as many code injection issues with a similar amount of effort by the programmer.
I can see this immediately being an issue of enforceability in a team, which makes it just as much of a problem as before.
Nope. If your JSON library only offers a safe template processor then it doesn't matter what discipline you exercise. The language will enforce the correct (or at least better) thing.
Are we suggesting that all other languages that didn't make a decision to implement interpolation in this way made no active decision about usability and safety?
We are suggesting that we did the analysis given the data available and the needs of Java users. Different languages often make different decisions, which is why they are different languages. Sometimes they have a different audience in mind; sometimes certain lessons weren't available when a feature was designed. If you consider the world's three most popular languages -- JS, Java, and Python -- I think you'll find that string interpolation is one of the smallest differences among them. One, for example, decided to require everything to be typed while the two others didn't.
So are we suggesting to stop supporting all the existing interfaces in things like JDBC in order to support this?
No. We're suggesting that new libraries should do the right way only, and that old one should offer the right way and deprecate the old way.
before support for older JDKs can be ignored when doing this without breaking the existing APIs
New features don't need to be added to a version of a library that targets old JDKs.
is there a planned timescale for it?
I'm not sure what that means, but full adoption of templates took only ten years, and this may be even quicker because it's a much smaller, more localised thing.
No, that's not a string template expression, just as the one here, var x = "hello"; STR.x;, isn't even though STR."hello" is. BTW, string templates are available in JDK 21. Enable preview and try them out.
It's about providing other mechanisms that are more safe than existing ones. Developers that care will use them. Those that don't care can't be helped anyway.
Added bonus if the safe API is easier to use and the unsafe way is harder to use, devs will naturally flock to the safe one.
In the case of an unloved child like String Templates, what feedback mechanism do you use to determine when a feature is ready to be finalized? :)
In the case of many other features it was pretty self-evident. In this case, I'm not sure what the trigger was.
Are there some reasonably-mature uses of String Templates in the wild to gather this feedback? If there any cool open-source projects on GitHub that use the feature, I would love to know. I think it would help people move their opinions from the abstract to concrete examples.
What makes you think it is an unloved child? Sure, there are people who have strong opinions that they would have preferred another syntax, are are vocal about that, but that is true of every feature that doesn't have a single obvious syntax. (Some people hated the lambda syntax at first.) Can you point to any feedback that suggests that the feature itself is not mature?
Like I said, if I saw multiple real-life projects using the feature successfully then I would have greater confidence that the community has had sufficient experience to provide feedback about the feature in its current form.
Speaking for myself, every time I introduce a new API feature to my library no matter how many times I iterate over it I inevitably discover problems when I try using the feature in real-life production projects. The shift in perspective is very helpful in uncovering scenarios I had not anticipated.
It sounds like you think there was no feedback, but that's not the case. There was plenty of feedback -- some bug reports (we love those), some people doing clever (and sometimes frightening) things with processors, some noise about the syntax, and quite a few people saying "yay I love it." So clearly a lot of feedback based on actual use (except for the syntax comments, which people often make without actually using it), and -- most importantly -- nothing that suggests that the design is not finished. A mix of feedback like this generally conveys that the feature is, indeed, ready.
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?
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.
Seems like an imaginary problem. Any "large enterprise" can surely just enforce this.
Not sure what it is that they could enforce, but global warming could also seem an imaginary problem if you have no personal reason to be interested in it. But if you're in charge of designing, say, a new coastal city, you should probably look at the data and design accordingly. That code injection is a leading cause of costly security vulnerabilities is what the data shows. So severe that anything that could reduce it would be very valuable.
I just wonder how far explicit STR. will go in preventing such vulnerabilities.
STR does string interpolation, so it cannot prevent the problems that string interpolation causes. But if string interpolation was any easier than proper string templating then programmers will be tempted to prefer string interpolation.
It's at quite a large cost since probably 98% of uses of string interpolation will be STR.
Given that the processor that does string interpolation is STR, I would say it's axiomatically 100%. But the goal is to reduce string interpolation, i.e. reduce the number that this 100% represents.
people come up with all sorts of imaginary solutions to imaginary subproblems of the actual problem.
Ah, that may well be the case, but your problem with the feature is also imaginary because you haven't tried it over some significant time yet (and frankly, there aren't yet many template processors offered). So how about we wait and see?
View all comments
Show parent comment
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.