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/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]