I use checkstyle to check if my java code respects the guidelines of our project.
However, we have one guideline that I cannot figure out how to check with this tool. We want to allow simple if (understand if with no else and no other conditional structure in it) to have no brace, like in this example :
// valid
if(condition) callFunction();
// invalid
if(condition) for(int i = 0; i < someValue; i++) callFunction(i);
// valid
if(condition) {
for(int i = 0; i < someValue; i++) {
callFunction(i);
}
}
// invalid
if(condition) callFunction();
else callOtherFunction();
This convention can be discussed, but this is the one we chose. It allows a reduced if syntax for very trivial cases, but ensures we have good indentation and block delimitation for more complex structures.
Any help with that would be really appreciated.
I’m also ready to do some code to perform this check if nothing is available, but really don’t know where to start. In last ressort, some tips about that will be appreciated too.
At the end, I did implement a custom check for checkstyle. Here is the source code if someone else is interested in it :