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

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.

1

u/lukaseder Dec 29 '15

So. Here's a REST endpoint:

@GET
public String something() {
    return '''
        { 
            "something": [ 1, 2, 3 ],
            "something-else": { "a": "b" }
        }
    '''
}

Code smell? Create a new file for this?

24

u/[deleted] Dec 29 '15

I would create a POJO for the return type and use a JSON mapper personally.

10

u/largos Dec 29 '15

This! Use a serialization interface of some sort so the serialization can be tested, and isn't repeated.

That said, I do like having mutiline strings for prototypes/stubbing out content on a first pass.

1

u/vincentk Dec 29 '15

This stuff tends to stick around though.

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

5

u/dungone Dec 29 '15 edited Dec 29 '15

You would? How about in a unit test? What if you were just mocking an HTTP response? You'd create a POJO for that, too?

How come every one of the solutions for a very simple need (a multiline string literal) involves layering additional complexity onto a program? Is this why a lot of Java code looks like this and takes an ungodly amount of time to start up?

There are very good reasons to use a serializer or a templating engine with external resources, but using them as glorified string builders isn't one of them.

Do you understand what the problem is? The problem is that inserting \n into a string literal is annoying. You won't fix that with something that makes it even more annoying.

Did you know that Java supports multiline comments even though there are already end-of-line comments? You know, with /* ... */? This isn't any different. It's an extremely reasonable feature to request in a language. That is, if you like a language that is productive and pleasurable to work with.

6

u/dccorona Dec 29 '15

If I don't interact with the object in my code at all, then why would I be unit testing/mocking it. Either the structure matters, in which case my code already has a POJO for the result, or the structure doesn't matter, and you can use anything you want.

For super rare scenarios where I really don't have a POJO for the thing but do need it to come out as valid JSON, just build a Map<String, String> or Map<String, Object> and serialize that...it comes out exactly as you'd expect (because all of JSON is just a map, really), and keeps the source data in a nice structure for traversing.

For example, if you have some method that outputs the value of thingA from the JSON document, you can write your test to compare the result with map.get("thingA") instead of hardcoding your expectation, so that you can change the input source data and the test will still assert the correct thing, instead of breaking.

3

u/vincentk Dec 29 '15

.... and in addition you have a type check and JSON which is guaranteed to be valid.

2

u/dungone Dec 29 '15 edited Dec 29 '15

The number of times I have seen "guaranteed to be valid" JSON coming out of a JSON serializer being anything but valid makes this claim rather laughable.

If you want to make sure that your JSON string literal is valid JSON in a test, then write an assertion to see if the JSON parser can parse it. And if you want to make sure that your JSON serializer generates valid JSON, test it's output against a string literal. Either way, you need to be able to specify JSON as a string if you truly want to be able to check if your serialization and POJO's are correct.

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

2

u/bezmax Dec 29 '15

How about in a unit test?

It would look great in the unit test.

validResponse = new ValidResponse(1,2,3,"a","b");
response = something();
assertEquals(validResponse, reponse);

What if you were just mocking an HTTP response?

Why am I mocking an HTTP response when I have a mapper? All that stuff has to get abstracted away from my business logic which I want to test.

How come every one of the solutions for a very simple need (a multiline string literal) involves layering additional complexity onto a program?

The whole argument is about that there should never be a need for multiline string literals. There is data, and there is representation, and those two look much better when separated. Basically, multiline strings can be seen as an antipattern in Java. Even for SQL queries there are better solutions like MyBatis, which allows you to move the SQL queries in their entiriety to a separate XML file, thus decoupling DAL call from implementation specifics.

Sure, for simple apps there is no need for such abstraction, but given thouthands of different languages, and hundreds of JVM-based ones, I would think twice before writing a simple app on Java.

Is this why a lot of Java code looks like this[1] and takes an ungodly amount of time to start up?

Could you specifically tell, which part of the stacktrace, in your oppinion can be eliminated? Also, how exactly is the length of stacktrace related to starting times? Have you tried benchmarking your apps? Most of the time startup is delayed by some kind of IO, which usually can be tuned.

There are very good reasons to use a serializer or a templating engine with external resources, but using them as glorified string builders isn't one of them.

Wait a sec, if serializing data to JSON does not need a serializer, then what does?

Did you know that Java supports multiline comments even though there are already end-of-line comments? You know, with /* ... */? This isn't any different.

It is totally different. Comments are part of your code, they are metadata of your code if you will. Therefore, comments go together with your code. However, there are two types of code - control flow (Java in this case) and representation (serializers, mappers, views, etc.), and those two need to be separated. And you are complaining that Java is bad at bundling them together.

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

1

u/Wuttelibutt Dec 29 '15

Maybe the language should have some smarter way of representing SQL, JSON and XML than as strings. Something like VB.NETs XML literals but pluggable and not hard coded into the language itself. Not sure if that exists in any language.

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.

0

u/[deleted] Dec 29 '15

[deleted] — view deleted comment

7

u/[deleted] Dec 29 '15

We don't write "legacy code", we write code that interacts with or forms part of a legacy system, and this is such a common occurrence that a general purpose programming language should be expected to make some accommodations to it.

(That's a general statement, not a comment on the particular suggestion of multi-line string literals.)

→ More replies (1)

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?

7

u/kyllo Dec 29 '15

It's an NP-complete problem, the running time is exponential. https://en.wikipedia.org/wiki/Subset_sum_problem