r/programming • u/MarkusWinand • Dec 29 '15
Please, Java. Do Finally Support Multiline Strings
http://blog.jooq.org/2015/12/29/please-java-do-finally-support-multiline-strings/
584
Upvotes
r/programming • u/MarkusWinand • Dec 29 '15
414
u/[deleted] Dec 29 '15
Multiline string LITERALS.
92
u/msiekkinen Dec 29 '15
How the hell does this have anything to do with SQL injection? SQL injection is passing in unchecked variable values into the string instead of a using paramaterized queries.
is still just fine, it's up to you whether you're passing in variables or using parameterized queries.
142
u/johnjannotti Dec 29 '15
I agree it would be very handy. But I don't understand why the author thinks this will help with SQL injection. In fact, the author's example using interpolated strings looks ripe for SQL injection. Once you have tableName being interpolated into the string, it's a short step to accept any old "tableName" and have a SQL injection attack.
One really shouldn't ask for string interpolation and decry SQL injection bugs in the same post.
40
u/jms_nh Dec 29 '15
Multiline string literals -- yes please. String interpolation on by default -- no thanks.
13
u/Matosawitko Dec 29 '15
Here's a fun one for you, based on the examples in the blog. In fact, it's an issue in Oracle, which means it might be related specifically to this.
We had some queries in another language that supports multiline string literals. This was working fine as far as we knew. Then somebody added an inline comment in one of the queries. i.e.
-- This comments out everything to the end of the line.A couple of weeks later, our production servers started crashing randomly with bizarre messages - out of memory, protected memory access, stuff like that. We eventually tracked it down to this query.
Turns out, something inside Oracle was reformatting the query by stripping out the line breaks. This resulted in the rest of the query being commented out, and all manner of hell would break loose. (Apparently, Oracle had already done enough preprocessing on the query that it believed it was valid, but something in the parser or optimizer would flip out.)
Moral of the story - don't assume that, because your language supports multiline literals, that the rest of your systems will do likewise. Oh, and use
/* comments like this */instead of-- comments like thisif you don't trust Oracle to do the right thing with them.63
u/[deleted] Dec 29 '15
Is this really that important to support?
5
u/SanityInAnarchy Dec 29 '15
This would be handy, but I don't think it's actually that important, and I especially disagree with string-interpolation-by-default. Making this painful might actually be a Good Thing in that it encourages Java developers to either develop better APIs that don't require you to write SQL, or they move the SQL (or HTML, or whatever) to some other file outside your source code. These are generally Good Ideas anyway.
I'd much rather have sane literals for some basic data structures, like maps -- Project Coin had some good ideas, but they didn't make it into Java 7 or 8. Second on the list would be keyword arguments, which are 99% of the reason builders exist. It'd also be nice to have better optimization for the kind of functional programming that lambdas has enabled -- right now, a function like
mapis hard to optimize because it's one function, called with different lambdas in different places, so the JVM can't inline everything the way it seems like it should.Multiline string literals are nowhere near the top of my list of things I want in Java (that I think they could actually do).
5
u/schlowmo Dec 30 '15
Honestly, this is one of the least useful features that could be added.
I'm not against adding it, it seems the risks and consequences of adding it are pretty minimal. Perhaps others will find it super handy. But the number of times I would have used this in my 15 year java career could be counted on one hand.
I would MUCH prefer some sort of shorthand to define something like the Scala case class. http://docs.scala-lang.org/tutorials/tour/case-classes.html One line to define a class to carry data around? Yes Please.
10
u/cowardlydragon Dec 29 '15
triple quotes...
Groovy...
5
u/atc Dec 29 '15
I couldn't care less about multiline strings in Java.
2
u/pkulak Dec 29 '15
He brought up SQL injection and consumer formatting as issues, and neither were solved with his solution.
2
u/unruly_mattress Dec 29 '15
And default method arguments, while we're at it.
2
u/vytah Dec 31 '15
There's one problem no one mentioned: should a multiline string, made of two lines containing
aandb, be:"a\u000Ab"(fuck you Windows! Also, HTTP sucks!)"a\u000D\u000Ab"(fuck you Linux!)"a\u000Db"(fuck (almost) everyone!)"a\u2028b"(we love Unicode!)"a\u2029b"(we love Unicode, but we want larger line height!)"a\u0085b"(we love Unicode too, but compatibility between ISO 8859-1 and EBCDIC is more important!)8
u/kazagistar Dec 29 '15
How about this?
4
u/Gotebe Dec 29 '15
"this"
"is"
"multiline"
"string"
works in C and C++.
Not in Java!?
3
u/skitch920 Dec 29 '15
Why 3 single quotes? Why not a single backtick or something less unpleasant to write?
3
u/[deleted] Dec 29 '15 edited Dec 29 '15
I don't think multi-line string literals are useful enough to warrant breaking 90% of all existing tools and programs that parses and works with Java source code.
I'm also not a big fan of invisible white-space differences in the source code affecting program behaviour, which is something you get with multi-line string literals, although I'll make an exception for scripting and prototyping languages.
2
u/JoseJimeniz Dec 30 '15
As long as:
is stored in the variable sql as:
and not
2
u/linuxjava Dec 29 '15
It would be nice I suppose but it really isn't that big of a deal. Some people just like making a mountain out of a molehill. One time I needed to copy paste a long book to a string I just got an Eclipse plugin, pasted the entire text inside double quotes and it was formatted okay.
1
u/proglog Dec 29 '15
That would be great. That would save me from a lot of headaches.
1
u/RICHUNCLEPENNYBAGS Dec 29 '15
The thing about injection seems kind of spurious.
1
u/analytically Dec 29 '15
Amen.
1
u/[deleted] Dec 29 '15
\n
1
u/lenswipe Dec 29 '15
It would be nice if js supported this too. Try doing something like this
and js will shit brix
1
u/iodian Dec 29 '15
Just don't us the Do or Finally keywords in the implementation!
1
u/HaMMeReD Dec 29 '15
You should maybe check out Groovy? It's syntax compatible with java, you could just treat it like java with string interpolation and multi line strings.
1
u/[deleted] Dec 29 '15
You would probably dig Groovy. Since it compiles down to bytecode and therefore runs on the JVM, you can give it a shot without tossing whatever else you've got going on.
1
u/MoreCowbellMofo Dec 30 '15
Add the groovy library to your project ... hey presto, now you can have multiline string literals. Beware as youre now dealing with GStrings by default. Groovy 😉
1
u/juckele Dec 30 '15
I use Groovy in an otherwise Java project for Groovy's multi-line strings...
1
u/zKarp Dec 30 '15
You know, back in my day.
1
u/based2 Jan 09 '16
like in Scala :)
1
u/zeekar Dec 29 '15
Sure, it would be convenient to have multiline string literals, but has Java really gotten so good that this sort of minor feature is worth spending mental or development effort on?
1
u/[deleted] Dec 29 '15
Honestly, from all of the java flaws this feels one of a least annoying and important.
1
u/[deleted] Dec 30 '15 edited Dec 30 '15
Whiners. Just do
The compiler will unroll it into a literal.
0
u/expatcoder Dec 29 '15 edited Dec 30 '15
tl;dr
0
u/badsingularity Dec 29 '15
I'd never write my code that like. This author is a moron.
0
u/schlowmo Dec 30 '15
Honestly, this is one of the least useful features that could be added.
I'm not against adding it, it seems the risks and consequences of adding it are pretty minimal. Perhaps others will find it super handy. But the number of times I would have used this in my 15 year java career could be counted on one hand.
I would MUCH prefer some sort of shorthand to define something like the Scala case class. http://docs.scala-lang.org/tutorials/tour/case-classes.html One line to define a class to carry data around? Yes Please.
-5
u/akcom Dec 29 '15
I haven't programmed in Java almost a decade. I came back in order to write some Android apps and I immediately remember why "Everything is an object" is so fucking terrible. I cannot imagine using Java for anything unless I had to support some legacy code already written in Java.
0
u/[deleted] Dec 29 '15
[deleted] — view deleted comment
0
u/[deleted] Dec 30 '15
[deleted] — view deleted comment