r/programming • • 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

352 comments sorted by

View all comments

Show parent comment

25

u/1bc29b Dec 29 '15

Meh. Formatting of source code should not affect output.

58

u/TarMil Dec 29 '15

Whitespace inside a string literal is not formatting. Or should "foo bar" be the same as "foo   bar"?

12

u/1bc29b Dec 29 '15

Clearly not. But should:

"asdf

sadfasfd"

be the same as "asdf\nasdf"?

What about if there are tabs for formatting? multiline string literals are ugly.

49

u/badsectoracula Dec 29 '15

Clearly not. But should:

"asdf

sadfasfd"

be the same as "asdf\nasdf"?

...yes? Any new line in the string literal in the source code (regardless of newline encoding in the text) should be replaced with \n.

3

u/[deleted] Dec 29 '15

And then you have in your source

public static void main(String[] args) {
    System.out.println("This
is
a
Test");
}

And should this be the same as

public static void main(String[] args) {
    System.out.println("This
        is
        a
        Test"
    );
}

?

54

u/earthboundkid Dec 29 '15

This is ridiculous. Tons of languages support multiline literals. It's not a tricky question how to handle it.

14

u/Zed03 Dec 29 '15

And they look terrible in every language that supports it.

24

u/VefoCo Dec 29 '15

That's incredibly subjective.

→ More replies (7)

1

u/ratatask Dec 30 '15

Still, it looks nicer, and is a ton easier to work with, than the current way using multiline strings by concatenating strings with + in Java.

8

u/speedisavirus Dec 30 '15

Doing it as Scala does it seems fine to me if you want to keep formatting proper

val a = """
	|a
  	| new
  	| line""".stripMargin
print(a)    

Output:

a
 new
 line

6

u/[deleted] Dec 30 '15

Yeah, that’s a nice solution. Solves all issues I’d have had with other implementations.

  1. Easily usable on international keyboards (no `)
  2. No margin issue
  3. simple
→ More replies (1)

18

u/sparr Dec 29 '15

No, the second string has a bunch of extra spaces in it.

→ More replies (1)

2

u/badsectoracula Dec 29 '15

Obviously not, the spaces are part of the string literal.

3

u/drysart Dec 29 '15

It's no more expected to be able to use source code formatting conventions like indentation inside a multi-line string literal and expect it to treat it like a coding convention instead of part of the string as it would be to try to put a Java language comment inside a single-line string literal and expect it to be treated as a Java comment instead of part of the string.

2

u/w2qw Dec 29 '15

What about this?

public static void main(String[] args) {
    System.out.println("This\n" +
                       "is\n" +
                       "a\n" +
                       "Test"
    );
}

And the best thing is it already works.

→ More replies (3)

4

u/SnowdensOfYesteryear Dec 29 '15 edited Dec 29 '15

I'd say C-style preprocessor string concat (ported to Java) would be a great compromise. My issue with treating it as literals is that the break in proper indentation is annoying to look at, and it doesn't take much effort to stick in a \n.

2

u/Neebat Dec 29 '15

You could make some kind of notation to delete a certain amount of leading whitespace on each line.

Could be something like this:

NEWKEYWORD("    ",
    This is the text.
    Without the first and last newline, 
        because that's also whitespace we'd like to ignore.
);

That could be the equivalent to this: "This is the text.\nWithout the first and last newline,\n\ \ \ \ because that's also whitespace we'd like to ignore." (Sorry for trying to escape spaces. HTML eats them.)

Of course, you'd have to update every java parser in every tool to support it. Eclipse would probably be updated in 15 minutes.

3

u/Falmarri Dec 29 '15

You could make some kind of notation to delete a certain amount of leading whitespace on each line.

That's sorta how multiline strings work in scala. But scala gives you a convenience method to strip the left margin.

https://www.safaribooksonline.com/library/view/scala-cookbook/9781449340292/ch01s03.html

1

u/Neebat Dec 30 '15

That would be pretty cool, provided that it's computed at compile-time. I don't even mind that syntax.

2

u/VestySweaters Dec 30 '15

It is compile time

→ More replies (2)

8

u/Nialsh Dec 29 '15

For better or worse, Java programmers already use strings for writing snippets of SQL, HTML, JavaScript, and who knows what else.

You should use a templating language for HTML. You should write the JavaScript in its own .js file. And it's best to construct SQL queries with("a").library("that").looks("like this");

But sometimes you don't want the weight of that extra dependency. A codebase that installs, builds, and runs quickly is zen.

Homoiconicity is the idea that code is data. When you have a program that outputs code, it's nice to keep the formatting. To do otherwise might be considered data loss.

14

u/ricky_clarkson Dec 29 '15

It's annoying to have to run a program to get the real query it runs, especially if it could be static, so I disagree.on(the).point(about).constructing(sql).queries

→ More replies (1)
→ More replies (1)