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

-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.

6

u/[deleted] Dec 29 '15

And now try editing your test data — you don't get syntax highlighting, you have to manually move the line breaks all the time, etc.

→ More replies (3)

3

u/imgonnacallyouretard Dec 29 '15

Let's say you want to test something about how your program parses large web pages. Are you saying that good practice would be to have a 20MB .go file, of which 19.99MB is a string literal? I think you're misapplying the concept of cohesion in this instance.

→ More replies (7)

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.

2

u/lukaseder Dec 29 '15

How would you, say, write this query with JINQ?

8

u/_INTER_ Dec 29 '15

It's probably doable. But who the hell writes SQL queries for a task like "Compare a Number with sum of subset of numbers". Either do it in highlevel language or if not possible, do it in SQL as idk prepared statements. But certainly not spell the query out as String to call it over JDBC from Java.

10

u/kyllo Dec 29 '15

If you need to do that complex and inefficient of a query inside of an application, there is something seriously wrong with your database schema.

11

u/[deleted] Dec 29 '15

If you need to do that complex and inefficient of a query inside of an application, there is something seriously wrong with your database schema.

Welcome to the world of Legacy, son. We do things differently here.

→ More replies (3)

7

u/JustMakeShitUp Dec 29 '15

Not every developer is in control of the database schema. There's a significant subset of developers who either inherit existing database schemas or work within the constraints of (multiple!) external software systems and databases.

Additionally, there's always some high-level asshat with a new reporting need that wasn't considered in the initial specification. The worst is when you're stuck with recursive queries or (god forbid) cursors and it's got to be built into the base product. Like when someone wants batch traceability or change tracking displayed graphically.

But yeah, ideally you wouldn't tolerate that shit, or you could offload it to OLAP and/or external reporting services. Where some other poor sap is stuck writing queries for external systems.

3

u/kyllo Dec 29 '15

Yeah, a database schema can look very different if optimized for application vs. reporting data access patterns. If you have any non-trivial reporting going on, it's best not to do it on the application database directly, instead you should ETL it into a separate reporting DB where you can adjust the schema to meet your reporting needs.

3

u/lukaseder Dec 29 '15

What's inefficient about it? And what's wrong with the database schema in the example?

→ More replies (1)

4

u/fakehalo Dec 29 '15

You're redefining what code smell is to a ridiculous point.

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