Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically reformat long-lined Java code to 80 columns and still compile? [duplicate]

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.

like image 360
John Murphy Avatar asked Dec 07 '22 06:12

John Murphy


1 Answers

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.

like image 60
MexicanHacker Avatar answered May 21 '23 14:05

MexicanHacker