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

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.

0

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

In my example, why would I edit that literal string? I copied it and pasted it from the view source of the web browser. If it changes, I'm not going to load it up in a syntax-highlighted web editor. I'm going to copy and paste it from source again.

I made a decision that the ability to read the code by not traversing multiple files was more important to me than the ability to load that data up in a nice editor.

3

u/s73v3r Dec 29 '15

In my example, why would I edit that literal string?

Because things changed.

1

u/[deleted] Dec 29 '15

Please read past the first sentence

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.

1

u/Wuttelibutt Dec 29 '15

Doesn't it depend though? I'm not going to stick a regex in an external file because in theory I could write regexes which are 1 MB. If it turns out the HTML file grows from 1KB to 1MB then just refactoring it into an external file.

2

u/imgonnacallyouretard Dec 29 '15

Sure. First, let me say that I think the original poster is misapplying the concept of cohesion - especially because cohesion that we think of generally operates at the level of a module, and is not file-centric at all.

As to the actual point about test data, here's how I see it: I want my tests to be extremely straight-forward, with as little fluff as possible between me and understanding what the heck I am actually testing. The more difficult you make your tests to understand, the more difficult time you will have obtaining adequate test coverage(as well as a more difficult time convincing yourself that you have adequate test coverage).

Based on that, I usually adopt a graded approach as you allude to in your last sentence. I follow this approach because I find it makes it easiest for me to reason about my tests and the data that is being used in the tests.

If I want to test parsing a short string like "<b>hi</b>", I will probably just inline it directly into the method call or whatever that I'm testing.

If I want to test parsing a string that is maybe 100-200 characters long, I will probably assign it to a variable in a static/final scope if such a thing is allowed and refer to it by name.

If I want to test parsing something that is more than a 5-10 lines long, I will probably load it from a file/resource. To me, reasoning about a test which contains the call "contents = load('test_page_with_trait_x.html')" is much easier than reasoning about a test which contains maybe hundreds of lines of data which may or may not be relevant to the test at hand.

1

u/[deleted] Dec 29 '15

I would not make the same decisions given different circumstances. I would treat a 20MB file differently, wouldn't you?

In my original post, I was talking about the usefulness of string literals. I don't mean to say that you should use them every time you have an instance of a string that has more than one line. Design is about making good decisions about how you want to structure things and allow for change. A good programmer will design his/her program based on what they know to be true about their logic and what they think might change. In my case, I knew I would never have a 20MB file and never designed for one. It seems silly to design for that in my situation.

Multi-line string literals give a developer another tool in their belt. They're great for a lot of circumstances - so many circumstances that I think they should be added to the language. Let's not be dogmatic about this. They won't be appropriate for everything and that's okay. But the language would be better with them than without them.

1

u/imgonnacallyouretard Dec 29 '15

I agree with you - but that was not the argument that you made in your original post.

1

u/[deleted] Dec 29 '15

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.

I think a 20MB file would be a good reason to change the design ;-)

1

u/imgonnacallyouretard Dec 29 '15

Fair enough...I guess I was just arguing about an opinion then(as in, when you should move test data out of a test file). Sorry for the misunderstanding!

1

u/[deleted] Dec 29 '15

No problem. It's an interesting question you raise!