4

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.

durron597
  • 30,764
  • 16
  • 92
  • 150
John Murphy
  • 367
  • 3
  • 13
  • 1
    If you are not willing to work with 80 cols width, is not courtesy what you're going to deliver but an automated splited code which not necessarily is humand-ready/friendly. While it may fit on the 80 cols the result not always will be useful. Anyway this is just a comment. – OscarRyz Jul 13 '09 at 22:18

4 Answers4

8

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.

Carl Manaster
  • 38,312
  • 15
  • 96
  • 147
MexicanHacker
  • 2,635
  • 3
  • 18
  • 20
7

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
  • 38,312
  • 15
  • 96
  • 147
  • 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) – John Murphy Jul 14 '09 at 15:42
  • 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 Jul 14 '09 at 15:54
  • 4
    You can run Eclipse formatter from command-line: http://blogs.operationaldynamics.com/andrew/software/java-gnome/eclipse-code-format-from-command-line.html – notnoop Jul 16 '09 at 14:30
0

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

ist_lion
  • 2,977
  • 5
  • 38
  • 70
0

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
  • 29,162
  • 12
  • 52
  • 83
  • 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. – John Murphy Jul 14 '09 at 15:48