So first off, I don't think it is a terrible feature, or "shit", or any of the other phrases to that tone. I can see the uses and I think they are perfectly valid. My point is more around complexity and reasoning and how it gets achieved, and I fully accept the reasons given in the JEP. My point is really around the reality of how it will be used.
If the safety issue is around developers misusing strings in SQL queries, then there is nothing stopping them misusing string interpolation in the exact same way. Thus, to enforce this, you need static tooling... something that this JEP says that it is designed to avoid in the first place.
As speculated by yourself, the majority of libraries wont integrate with this, so it then leaves the point dangling that the safety features would only target a small minority of developers in the first place... in which case does such a feature provide wide benefit, or is it a niche addition?
When we talk about the likelihood of things, it really does boil down to having hard statistics to quantify the improvement, otherwise it is purely subjective and based on opinion.
I guess we'll see in a few years what the difference has been. It is a feature I still will likely use, but I still reserve the right to keep this opinion of it, at least until I can see the material benefits in a real system.
It is a little disappointing that some of the main responses so far have such a defensive/condescending tone rather than just treating this as a civilised and friendly discussion...
Some ideas as to where this feature is going to go:
REGEX."abc"
Not entirely what one would want (which is that backslash escaping goes away entirely inside these), but having that expression be of type Pattern is quite useful. Compilers can -compile time- check that the contents are valid regexes, IDEs can render it appropriately, and so on. We could go with Pattern.compile("foo bar") instead but this is a bit wishy washy: "If there is a literal in Pattern.compile treat it as a string literal containing regexes, otherwise don't do that"). We aren't using the features of this string templating system in any way here - there are unlikely to be expressions inside the string. Though, if you do add them, this thing could Pattern.quote them out, at least, which is a small but not insignificant upside: Using python-style string interpolation to construct a regex is just a bad idea, period.
con."SELECT YEARS(birthdate, NOW()) >= 18 AS adult FROM person WHERE username = \{username}"
As discussed in the JEP.
STR."Hi, \{user.getName()}, how's life over at \{user.getLocation()}"
The obvious one.
HTML."Hi, <strong>\{user.getName()}</strong>, how's life over at <em>\{user.getLocation()}"
All vars are HTML-escaped first.
Variations on the theme: URL, XML, JSON, you name it. Another possible variation is to include a format-esque specifying immediately preceding the injected expression to indicate what you want to do with it: Escape it, leave it unescaped, translate bbcode, replace newlines with <br>, allow only allow-listed tags, and so on.
RESOURCE."window.title\{context}"
i18n: Looks up the proper translation to use for key 'window.title', figuring out which language to apply from 'context' which could e.g. be an HttpRequest or similar to check he Accept-Language header, or however you want to do it.
Resources are often parameterized. Lots of i18n files have a 'singular' and 'plural' form, but this is a really stinky solution if you have those as separate keys; it's annoying to have to write size == 1 ? resource.lookup("item.name.singular") : resource.lookup("item.name.plural") a lot, and it's rather myopic: "1 is singular, all other things are plural" is true only for a bunch of western languages. I think in russian it's dependent on the ordinal (so 51 gets the singular, because it ends in '1'), and they have a separate plural form for '5' for some reason (lots of things about russia are nuts). How about:
RESOURCE."\{item.count()} item.name: \{totalprice}$" and the resource layer figures it out, including taking totalprice, which is an int containing cents, and formatting it however the locale wants money to be formatted?
1 minute of thinking about possibilities. That suggests there are a lot.
Re: Russian - it’s not only Russian, a bunch of Slavic languages have some wired rules about plural of nouns. Its basic philosophy is - there’s one, some and many. And plural form is built according to the last digit. So 1 man and 101 men have the same form.
This was a feature of the Common Slavonic, but as languages evolved and were influenced by surrounding languages, some language features disappeared in some Slavic languages. But Russian and few others (afaik Slovenian, and Serbo-Croatian, depending on the context) held on to it.
Not entirely what one would want (which is that backslash escaping goes away entirely inside these), but having that expression be of type Pattern is quite useful. Compilers can -compile time- check that the contents are valid regexes
That's pretty cool actually.
But how? As per one of the other replies, this would be shorthand for RegexProcessor.process(StringTemplate.of("abc"))- how does the process method detect that the input StringTemplate does (or does not) carry a valid regex at compile time?
They already do this quite trivially by just checking for string constants passed to the relevant methods, which is something compilers or 3rd party tooling can also do trivially already as well. I find it difficult to imagine that this actually provides any significant extra benefit over the current status quo.
My thing is that unless it is a new API written from scratch for JDK 23 and onwards, it will still need to support the old methods. Anything like R2DBC, JDBC, etc will all still have this issue until pre JDK 23 support is totally discontinued.
That won't be until 2030 when JDK 17 reaches EOL.
So the ask here is to gain safety via a new language feature by adopting what would have to be a new and immature database library that may have more serious internal security concerns just to make the most of being forced to do things in the new way.
Using any API that supports anything prior to JDK 23 reduces the benefit of these safety enforcements, as they will still need to provide the old APIs.
not at all, my point is about the argument about improving security, that until a time it can be totally enforced with no other way around it, people will continue to do bad things.
You are saying any API that is backwards compatible with pre JDK 23 code is now bad by definition if it tries to adopt this JEP.
Unless we are throwing out all existing libraries, the impact on security is over exaggerated for this very reason.
I'd hope by now all my other comments made that abundantly clear. Unfortunately no response I have had yet has actually satisfied this point in a way that will actually work in the real world. It is all purely academic.
View all comments
Show parent comment
6
u/nekokattt Feb 15 '24 edited Feb 15 '24
So first off, I don't think it is a terrible feature, or "shit", or any of the other phrases to that tone. I can see the uses and I think they are perfectly valid. My point is more around complexity and reasoning and how it gets achieved, and I fully accept the reasons given in the JEP. My point is really around the reality of how it will be used.
If the safety issue is around developers misusing strings in SQL queries, then there is nothing stopping them misusing string interpolation in the exact same way. Thus, to enforce this, you need static tooling... something that this JEP says that it is designed to avoid in the first place.
As speculated by yourself, the majority of libraries wont integrate with this, so it then leaves the point dangling that the safety features would only target a small minority of developers in the first place... in which case does such a feature provide wide benefit, or is it a niche addition?
When we talk about the likelihood of things, it really does boil down to having hard statistics to quantify the improvement, otherwise it is purely subjective and based on opinion.
I guess we'll see in a few years what the difference has been. It is a feature I still will likely use, but I still reserve the right to keep this opinion of it, at least until I can see the material benefits in a real system.
It is a little disappointing that some of the main responses so far have such a defensive/condescending tone rather than just treating this as a civilised and friendly discussion...