Rule for blank lines after curly braces

Our coding guidelines state their shouldn’t be any unnecessary blank lines. A consistent layout of the code enhances readability.
It would be nice if we could have SonarCloud enforce this rule.

The rule is that:

  • after a { there should not be a blank line
  • before a } there should not be a blank line
  • there should not be 2 consecutive blank lines (does not involve curly braces though, so perhaps this should be separate rule?)

Non compliant code:

void MyMethod() {

  if (condition) {
     doSomething();

  } else {
    doAnotherThing();


    doMore();
  }

}

Compliant code:

void MyMethod() {
  if (condition) {
     doSomething();
  } else {
    doAnotherThing();

    doMore();
  }
}

I think this rule can be applied for any language that uses curly braces.

Hi,

I know this is obvious to you, but it’s not obvious to me. :sweat_smile:

What language are we talking about?

 
Thx,
Ann

I think this rule can be applied for any language that uses curly braces.
So, C/C++/C#, Java(script) and probably many more that I am unfamiliar with.

Hi @krlx,

We try to avoid defining rules focusing only on formatting, mostly because other tools (such as IDEs or clang-format) already handle formatting in a transparent way that provides a seamless user experience (they basically format the code as you type it, we have totally different workflows).

We would have a hard time, with no real benefits, if we tried to compete with those. We focus on detecting more advanced issues.

Hi Loic, that’s understandable. Too bad though, because I don’t want to depend on some developer’s IDE being properly configured. This means I need to bring in another tool in our build pipeline/server, I quite like the way SonarCloud is easily configurable and can deal with an existing codebase full of issues and can only check the new stuff. I doubt I will find this in other tools.

1 Like

At least for C and C++, you’ll find that clang-format is also highly configurable (maybe too much, getting the config that works for you might require some back and forth). Additionally, it can be used both on the command line for CI and interactively, integrated inside most IDEs.

It’s not really good at focusing on new code only. What we did when we started using it was reformat all our code base in one huge PR. We know it’s not always possible everywhere, but it worked well for us.