Question about code style

Is there a code formatter that automatically applies the projects code style rules to the code? Maybe an Eclipse/IntelliJ code formatter setting xml?

If there isn’t, can we make one and apply it to the whole codebase, as there is some code fragments now with varying code styles, the opening brace “{” sometimes starts on the same line and sometimes on a new line, I also think I’ve seen different styles of else / catch blocks, some start on the same line and some on a new line.

Thus spake Flint1b:

Is there a code formatter that automatically applies the projects code
style rules to the code? Maybe an Eclipse/IntelliJ code formatter
setting xml?

If there isn’t, can we make one and apply it to the whole codebase, as
there is some code fragments now with varying code styles, the opening
brace “{” sometimes starts on the same line and sometimes on a new line,
I also think I’ve seen different styles of else / catch blocks, some
start on the same line and some on a new line.

I don’t have any formatter set up, but should you set one up it should:

  • use 2-space indentation
  • no tabs
  • put opening braces for control blocks with their keyword
  • put closing braces for control blocks on their own lines


J.

Oh my! We must be soulmates! :smiley:

With us using different IDEs, editorconfig and their 5-10 settings that are supported by all IDEs being quite boring, I think checkstyle is the way to go. I made a first proposal in a PR.

No tabs, sure thing. Indentation 2, why not. Left/right curly braces, sure, one of the (very) few good things that C++ gave the world :smiley:

Else/catch on new line, uncommon, but why not.

The worst abomination though is this:

condition1 &&
  condition2 ||
  condition3 ||
  condition4


if condition ?
  expression1 :
  expression2;

The operators belong in the next line, especially in the case of several ANDs or ORs, then one can neatly copy-paste-delete whole lines together with the operator and the condition that follows. If the operators are not on the previous line it looks very weird, why would someone do this, it’s like writing the regular if-else like this:

if (condition1) {
  foo(); } else if (condition2) {
  bar(); } else if (condition3) {
  baz(); }

For now this operator-in-wrong-line is reported as a warning, if the C++ developers want to take Vassals code to the obfuscated C code contest some day I guess we can move this warning to info or remove it entirely, though my personal suggestion is to keep it as warning and some day even upgrade to error and change it to regular Java style.

And I guess I don’t need to say anything at all about C-style names with all lowercase letters separated by underscore :smiley: