Java formatting convention for new project

52 views Asked by At

I am starting a new project which might be open-sourced later on and/or at least get some external contributors during its life-time.

I am now thinking about what the best approach to code-style / auto-formatting would be. I am a strong supporter of only having auto-formatted code committed to a project, as this eliminates the differences between individual developers and helps keeping individual commits clutter-free of reformatting issues.

My first approach was to use Eclipse built-in style for the project, but I really don't like the default style, because I think line-break at 80 characters is way out-dated for today's screen resolutions. Also, as the name suggests, it's available only for people using Eclipse as IDE.

So I was also thinking about using my own formatter settings and checking the exported settings into the project's repository so that any contributor can pick them up. Again, this would force most people to use Eclipse, as I am not aware of any formatting definition that can be read by multiple IDEs.

Any hint how this is handled in other projects? I searched some github repositories, but to me it seems that this issue is more or less ignored by a lot of projects.

I do understand that this question may be border-line for Stack Overflow, as I don't know if a definite answer is possible and if this triggers a discussion, but it is something I often struggle with when starting a new project.

2

There are 2 answers

3
Edwin Buck On

While screens grow wider, they don't seem to grow taller.

Whatever you other drivers are, preserve vertical space. Put { and } on lines containing other language key words, if you can.

In any case, use a maven plugin or other automated tool in your compile chain to enforce the rules that you care about. That way they are unambiguous.

Also don't create too many rules that don't matter. Each rule costs time to make the code comply.

0
Mudassar On

I understand your concern and in my opinion the best approach is to create code formatting preference file which can be shared along with the project.

For example in eclipse Using a file explorer, navigate to //.settings and copy org.eclipse.jdt.core.prefs to a new location. This file contains all  your formatting settings. Hence this can be shared to maintain the code formatting consistencies. If not that then you might have to rely on the editor specific code formatting.

I definitely look forward to other expert opinion on the same if what I have shared is not optimal as per the requirement.