Question

This isn't so much a question with a problem with a section of code but how it should be correctly laid out when a single lengthy sting is forced onto a separate line.

For example say I have the below line of text in a small text area.

JOptionPane.showMessageDialog(null, "The start position must be > 0 and the stop 
position must be < 12, please try again.");

I realise the function of the above code is not hindered by the format but is this the correct way to lay it out? A friend mentioned legally it should be done with concatenation, for example.

JOptionPane.showMessageDialog(null, "The start position must be > 0 and the stop"
                              + "position must be < 12, please try again.");

I'll be grateful for any advice.

Was it helpful?

Solution

"Legally" speaking, both are equally correct. If one wasn't legal, it wouldn't compile.

As far as readability is concerned, I usually side with your friend (explicitly split your string into two lines and concatenate it). According to the Java Code Conventions published by Oracle, lines should not exceed 80 characters. I (and several people agree) that this is simply too short and 100 or 120 character line limits should be used.

Basically, if your line of code (containing the long string) is going to breach your line limit, I recommend your friend's solution of concatenating two strings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top