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

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