views:

559

answers:

4

My apologies if this is a duplicate, I've seen a couple threads on the subject of 80-column code, but didn't see an answer to this specific problem:

My team is developing Java code in a couple different IDEs, with differing numbers of columns. It's worked well so far, but we've been asked to deliver compilable code to a partner with the source limited to 80 columns. We'd like to do this as a professional courtesy, but we're not able/willing to work with 80-column limited code on a day to day basis. Are there any tools available that will take Java code, and intelligently add line breaks so that it fits within 80 characters and still compiles correctly? Ideally, I'd like it to be one more automated step in our build process, to go recursively through our source directory for .java files and produce a copy with the new formatting.

I'm not interested in the virtues of 80-column code or editors that do or don't word wrap, just in seeing whether there is a quick fix that will make everyone happy without anyone having to change the way they do things.

Thanks!

+1  A: 

It's not as automated as you like, but in Eclipse you could define an 80-column profile (Preferences -> Java -> Code Style -> Formatter), then in the Package Explorer, right-click on your root package, and choose Source->Format.

Carl Manaster
Thanks; after asking around it sounds like this is what people are willing to do.(And especially thank you for not lecturing on the merits/failings of 80-column code)
JMurphy
You're welcome. I could see, under other circumstances, being a little more dogmatic about it, but you clearly weren't advocating for the 80 columns; it's just something you've got to do and want the least painful way to go about it. Happy to help.
Carl Manaster
You can run Eclipse formatter from command-line: http://blogs.operationaldynamics.com/andrew/software/java-gnome/eclipse-code-format-from-command-line.html
notnoop
+5  A: 

Intellij and eclipse and almost any IDE will do that. But...

Maybe you want to ask yourself if you want to automate that, even if you do that in an automated way, let's say maven, the code could be break up in a way it's harder to read than it was before, it can be breaking in the middle of an important line or method and you don't want that even if it compiles.

What we have done in the past is set a couple of .xml file with the settings of eclipse or intellij (they both allow you to export the settings), then with maven use the checkstyle plugin to enforce developers to have this settings; it will allow you to do it in a day by day basis always with a developer deciding when the code should break the line.

And lastly, maybe you want to use 100 or 120, they are the new 80.

MexicanHacker
+1 for "100 or 120 are the new 80". Love it.
Carl Manaster
A: 

I would suggest using code formater in Eclipse to get the 80 columns issue taken care of.

Then to make sure you're not missing anything - take a look at checkstyle to see that you're adhering to those standards

PSU_Kardi
+1  A: 

Any modern IDE will auto-reformat according to your preferred style, but I would only do this on code that's seriously unformatted, else it can wind up looking less readable. In particular if the original author did a lot of custom formatting for clarity, the result will lose it.

And I second the suggestion to move to 100-120 characters. The 80 character limit was suitable for CRT terminals (24 lines x 80 columns), and for the 1928 IBM punch card standard.

Jim Ferrans
Yeah, the objection has already been raised that pre-beautified code will be ugly, but the person receiving the code doesn't care as much about that: for various reasons he has to use an 80-character terminal to read.
JMurphy