Reputation: 82589
I'm facing a particular line that is 153 characters long. Now, I tend to break things after 120 characters (of course, this is heavily dependent on where I am and the local conventions.) But to be honest, everywhere I break the line just makes it look bad. So I'm looking for some ideas on what I should do for it.
Here's the line:
private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper = new HashMap<Class<? extends Persistent>, PersistentHelper>();
I'm open to both ideas about how/where to break the line (and why), as well as ways to shorten the line itself.
We're not a Java shop, and there aren't local conventions for this sort of thing, or obviously I would simply follow them.
Thanks!
Upvotes: 52
Views: 206507
Reputation: 2083
In general, I break lines before operators, and indent the subsequent lines:
Map<long parameterization> longMap
= new HashMap<ditto>();
String longString = "some long text"
+ " some more long text";
To me, the leading operator clearly conveys that "this line was continued from something else, it doesn't stand on its own." Other people, of course, have different preferences.
Upvotes: 65
Reputation: 2500
This is how I do it, and Google does it my way.
=
and for ,
.In your case, since you're using 120 characters, you can break it after the assignment operator resulting in
private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper =
new HashMap<Class<? extends Persistent>, PersistentHelper>();
In Java, and for this particular case, I would give two tabs (or eight spaces) after the break, depending on whether tabs or spaces are used for indentation.
This is of course a personal preference and if your project has its own convention for line-wrapping then that is what you should follow whether you like it or not.
Upvotes: 28
Reputation: 4798
I think that moving last operator to the beginning of the next line is a good practice. That way you know right away the purpose of the second line, even it doesn't start with an operator. I also recommend 2 indentation spaces (2 tabs) for a previously broken tab, to differ it from the normal indentation. That is immediately visible as continuing previous line. Therefore I suggest this:
private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper
= new HashMap<Class<? extends Persistent>, PersistentHelper>();
Upvotes: 2
Reputation: 5120
Uses Guava's static factory methods for Maps and is only 105 characters long.
private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper = Maps.newHashMap();
Upvotes: 2
Reputation: 93177
IMHO this is the best way to write your line :
private static final Map<Class<? extends Persistent>, PersistentHelper> class2helper =
new HashMap<Class<? extends Persistent>, PersistentHelper>();
This way the increased indentation without any braces can help you to see that the code was just splited because the line was too long. And instead of 4 spaces, 8 will make it clearer.
Upvotes: 7