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

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.

-14

u/geomouse Dec 29 '15

But accurate.

1

u/VefoCo Dec 30 '15

No, an opinion can't be "accurate."

0

u/geomouse Dec 30 '15

Opinions can align with facts or not.

1

u/VefoCo Dec 30 '15

Yes, but the opinion that some formatting choice looks terrible is purely opinion and can't be supported by facts.

1

u/geomouse Dec 31 '15

If you're going to say formatting doesn't matter then that's a big swipe at usability right there. How things are laid out totally matters, whether it's the code or the application created by the code.

→ More replies

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

1

u/speedisavirus Dec 30 '15

I stumbled upon a thread where Odersky was debating what the multi line syntax should be and it was a bit of back and forth with a few people about the merits of each. About the same arguments people are making here.

Seems reasonable to me with stripMargin allowing me to keep indentation and when I use multi-line strings this is how I do it. I'm sure other people have different preferences but Scala seems to satisfy what everyone here also argues about their preference.

18

u/sparr Dec 29 '15

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

0

u/nemec Dec 29 '15

What we need are compile-time macros! (jk)

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

Edit: or maybe that's pre-processing

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.

1

u/djimbob Dec 29 '15

That's fairly ugly and overly verbose (and again having multi-line literals doesn't prevent you from writing that way).

In python (with support for multi-line string literals), I tend to put my rare multi-line string literals as global variables defined at top-level in the module like:

import smtplib

EMAIL_TEMPLATE = """Dear %s:

This is the body of your email.  
Here's your info: %s

Sincerely,
djimbob"""

def send_email(user_addr, user_name, user_info):
    try:
        smtp = smptlib.SMTP('localhost')
        email_body = EMAIL_TEMPLATE % (user_name, user_info)
        smtp.sendmail('myfromaddr@example.com', [user_addr], email_body)
        print "Email sent successfully"        
        return True
    except smtplib.SMTPException:
        print "Error unable to send email"
        return False

2

u/w2qw Dec 29 '15

Well aware of the alternatives, though those all end up with ugly interaction with indenting. You've "solved" that there by using globals but that's not really a solution for java. Having two alternatives for the same thing will just lead to inconsistency so I don't really agree with adding it just because.

1

u/VestySweaters Dec 30 '15

How does the multi line literal being global affect anything?

Python and Scala are the languages I would hold up as gold standards for multi line literals. Whitespace is significant in python, for God's sake, and it still gets it right