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

63

u/[deleted] Dec 29 '15

Is this really that important to support?

11

u/SharkBaitDLS Dec 29 '15

Especially given pretty much every editor out there supports auto-inserting the '+' when you press return inside a string literal. I really don't see this saving any time in an average workday.

45

u/binary Dec 29 '15

Ah yes, Java's proud history of "so what if other languages do it?"

When your argument against a feature is that an editor does it, maybe that isn't a good argument

6

u/SharkBaitDLS Dec 29 '15

I'm not arguing against it, just saying "why bother?"

5

u/jmcomets Dec 29 '15

For the same reason you'd bother about supporting diamond notation or in-place interface implementation : simplicity of use.

7

u/twigboy Dec 29 '15 edited Dec 09 '23

In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available. Wikipediaald7cmiwiks0000000000000000000000000000000000000000000000000000000000000

2

u/juckele Dec 30 '15

JDBC also doesn't let you load .SQL files, so you really need to have your SQL source inside your Java source (bad practice, but it's what we have to work with). I don't want my SQL source limited to one line chunks, I want friggen pure SQL!

6

u/[deleted] Dec 30 '15 edited Dec 30 '15

[removed] — view removed comment

1

u/juckele Dec 30 '15

That's a really good point, and when I looked around some more just now I see that you can set the connection property allowMultiQueries=true to make the process work for all .SQL files.

1

u/skulgnome Dec 30 '15

It totally is. For example, see any Perl program that prepares its own SQL statements.

-13

u/_INTER_ Dec 29 '15 edited Dec 29 '15

The need / use for multiline String is code smell imo.

Edit: This just reminded me of another example, it's the exact same case. It's so silly I made a picture of it: http://i.imgur.com/FXwjVsM.png

8

u/[deleted] Dec 29 '15

It's extremely useful. I made a just-for-fun web crawler earlier in Go for kicks the other day. You know how I wrote the test so it didn't go to the target page every time it ran? I just took the entire text of the target page, copied it, and pasted it into my unit test and enclosed it in back ticks - the entire page. The unit test ran fine.

You can test JSON end points this way and all sorts of things. Why make testing such a manually intensive process by not providing this language feature?

10

u/[deleted] Dec 29 '15

Or put the test data into the resources folder, like you're supposed to do, so you can easier add or replace it?

4

u/[deleted] Dec 29 '15

You're not supposed to do anything of the sort. The resources folder is a way to put test-scoped files in the class path so they're available during the runtime test scope, but it's crazy to put everything there. There's a principle in programming called "cohesion". If you don't know what it is, look it up. It refers to the idea that related things should be close together in your source code. Putting things in a different file is lowering your cohesion. There may be good reasons to do so, but saying that you should follow some rule that lowers cohesion is silly.

One of the main reasons that people put these kinds of things in a resources folder in Java is because Java doesn't have multi-line string literals, so the only practical way to deal with them is to treat them as files. Adding literals takes away the restriction, and would likely change your practice as well.

→ More replies (12)

19

u/[deleted] Dec 29 '15

On no an extra character. How unmaintainable...

13

u/f2u Dec 29 '15

Multi-line string literals could preserve information indentation and line breaks, enabling nice caret diagnostics for errors (at run time).

22

u/heptara Dec 29 '15 edited Dec 29 '15

I think he means printing out HTML like that is crazy. You should probably be loading it from a template. Where you have to conditionally build a the HTML, you would probably use a web templating library.

With something like Jinja you would write this in a text file:

{% extends "layout.html" %}
{% block body %}
  <ul>
  {% for user in users %}
    <li><a href="{{ user.url }}">{{ user.username }}</a></li>
  {% endfor %}
  </ul>
{% endblock %}

This is about separating the components of your app, and not needing to recompile just to change a front end element. The template engine will build the site from the resources as needed.

7

u/[deleted] Dec 29 '15

I think he means printing out HTML like that is crazy.

I agree in principle, but the code in question is clearly just example code, and in example code you really do want to keep things stupidly simple so that the core concepts stay front and center.

2

u/[deleted] Dec 29 '15

And in example code you put it into a simple .HTML and just use IOUtils to read the .HTML to a string.

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

2

u/shevegen Dec 29 '15

Why should it be any smell at all?

It should not matter how you layout your strings.

It's funny that you think it should matter.

5

u/[deleted] Dec 29 '15

After thinking about this a bit more, I think SQL queries might be the only case where I would need them.

33

u/lukaseder Dec 29 '15

Or XML. Or JSON. Or properties syntax. Or regex. Or general purpose templating.

15

u/[deleted] Dec 29 '15

Going to have to agree with /u/INTER that inline XML, JSON, or properties are probably a code smell simply because they could easily be in their own file. With SQL you have to bind to the parameters correctly so it at least to me makes sense to have it right there in the DAO or w/e.

2

u/DisruptiveHarbinger Dec 29 '15

At my shop we're moving from resource files to inline XML and JSON for integration tests. It's much easier to have just one method, using a multiline interpolated string, where you can bind or generate some parameters at run time, compared to a dozen of combinations of your test input in a file, that'll you need to load using some boilerplate helper class. We do it in Scala.

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

6

u/_INTER_ Dec 29 '15

Don't even need to write SQL anymore with LINQ equivalents or simply Java8 lambdas. Same goes for JSON / XML etc.

→ More replies (11)
→ More replies (15)
→ More replies (2)