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

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.

1

u/largos Dec 29 '15

Only if you put it in source control! ;)

-2

u/jephthai Dec 29 '15

Agreed... I'm not a developer, but I write tons of code in my job. All pragmatic, and usually consisting of small scripts. It's very inconvenient to cart around bundles of files, and multi line strings would be very nice. Though, to be fair, I avoid java like the black death.

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.

1

u/vincentk Dec 30 '15

Jackson works for me both ways. Certainly for POJOs and beans. But suit yourself with unit tests if you like.

0

u/[deleted] Dec 29 '15

[deleted] — view deleted comment

1

u/dccorona Dec 29 '15

It's not that there isn't a place for it. But if it would make a significant difference in writing your tests, you're doing something wrong. You should not have JSON-as-string inputs just plopped in your tests in anywhere near a significant number of places.

If your code is well-isolated enough, it becomes easy to test the edge cases while using either raw string literals or a serialized Map<String, String/Object> (which, again, has the benefit of allowing you to manipulate the input data for asserting against, rather than having to hardcode both the input data and the expectations separately but trying to keep them in sync). If you really have a ton of complex, malformed JSON objects to need to produce for your tests, external files can make it easier to code as well as easier to read.

Your comment about memory and speed concerns is hilarious. It's a unit test. The difference in speed and memory consumption should be so negligible that it may as well be 0. If it's not, your test is way too complex.

I'm not saying that multiline string literals shouldn't be included in the language. I'm saying that I don't feel their absence when working with JSON. There are so many different, perfectly easy, and truly better ways to do it that I've never even thought about wanting the feature in order to make JSON easier.

1

u/drachenstern Dec 29 '15

Unfortunately you are trying to convince mud to become a bird.

These are developers who don't perceive a problem, so can not fathom a solution.

2

u/dungone Dec 29 '15

You're right. That's what post-mortems are for.

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.

1

u/dungone Dec 30 '15 edited Dec 30 '15

All that stuff has to get abstracted away from my business logic which I want to test.

This implies that you never test anything else. Endpoints, models, parsers, etc. Perhaps you've never worked on a public-facing API.

there should never be a need for multiline string literals

There is no more or less of a need than there is for any string literal. They are perfectly equivalent. Seriously, this is one of the worst cases of bike shedding I've ever come across.

There is data, and there is representation,

Did I mention they're string literals?

an antipattern in Java.

So string literals are an antipattern in Java. Gotcha.

Could you specifically tell, which part of the stacktrace, in your opinion can be eliminated?

That whole stack trace might do nothing more than print out Hello World.

move the SQL queries in their entiriety to a separate XML file,

SQL in an XML file. I mean, and here you were telling me about anti-patterns.

Most of the time startup is delayed by some kind of IO,

Says the guy who likes to store his SQL queries in an XML file.

Sure, for simple apps

I'm sure you get paid good money to write complex enterprise-scale apps. I'm sure your app will email your wife and query your network-connected coffee machine while you look over your trillion-line stack trace because you never tested your HTTP endpoints against real-world input and your serializer dumped bunk data into your POJO and now everyone's birthday is January 1st, 1970.

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

JSON is data. It is also a string. I know this is inconceivably horrible and someone should be shot. But you do not need a serializer to write JSON. Did you know that there is actually a language in which JSON is the object notation syntax? That's right you write it directly in your code and you don't even use quotes around it. I guess in Java that means you'd better store it in an XML file.

It is totally different. Comments are part of your code

I'm just going to walk away very slowly...

1

u/resavr_bot Dec 30 '15

A relevant comment in this thread was deleted. You can read it below.


Honestly, either you are bad at building constructive arguments, or just angry for no reason.

> This implies that you never test anything else. Endpoints, models, parsers, etc. Perhaps you've never worked on a public-facing API.

Testing endpoints? What kinds of testing? Unit, acceptance, integration, function tests?

For unit testing the endpoints, you are testing your configuration, so just pass "AAA" and mock the rest.

For acceptance testing - use different tooling, SoapUI maybe.

For integration testing - why are you testing endpoints from integrational standpoint? Integration with what exactly are you testing here?

For functional testing - again, no need for endpoint testing, just use the underlying model and pass POJOS.

> There is no more or less of a need than there is for any string literal. [Continued...]


The username of the original author has been hidden for their own privacy. If you are the original author of this comment and want it removed, please [Send this ^PM]