How it works Static code analysis Technical paper

Choose a category:

Follow up to ‘missing braces in If statements’

August 1st, 2008 by Rich Sharpe. Posted in Coding Standards, Software Quality, Static Analysis

Earlier this week on Javalobby, I posted an extract from our monthly newsletter regarding our analysis of the ‘missing braces in If Statement’ rule firing and the potential bug involved.If you omit braces and use Static Analysis tools it is a problem actually finding these bugs. Why? Because you probably have the rule turned off!Having any tool tell you of violations in your code purely due to a stylistic preference will result in thousands of false positives and eventually demotivate the developer from using the tool, therefore, for the other good causes these tools promote, in this case it is probably better to switch the rule off.Hopefully Unit Tests are in place to cover these areas if static analysis is not used. The only other way to find these bugs is manual code review (laborious, time consuming and introduces human error i.e. the bug may be missed anyway) or debugger tracing.Another interesting aspect about the post was the comments. Many of those who omit braces in If Statements do so for readability purposes. Psychologically, this may mean that these developers see readability as a higher aspect of quality than possible fault-prone code. I’m not stating that readability is not important, far from it. However from a business perspective, having the code released with less faults is a higher quality perspective.

6 responses to “Follow up to ‘missing braces in If statements’”

  1. Bernd Eckenfels says:

    One can do some source based heuristics checking for empty lines and indention. Candidates for problems:

    if (a)
    b;
    c;

    candidates for no problems:

    i (a) b;
    c;

    if (a)
    b;
    c;

    if (a)
    b;

    c;

    This is by no means perfect, but if someone choses to turn off the braces rule, he still can get some warnings about code which delivers visually the wrong semantics.

    BTW: anything going on in Bugpedia - we could move the discussion there. I have another comment on Comment/LOC there and no followup yet.

    Greetings
    Bernd

  2. Bernd Eckenfels says:

    sorry.. indention was killed.

  3. Kylie Batt says:

    Какие отличные слова…

    Главный бухгалтер If you omit braces and use Static Analysis tools it is a problem actually finding these bugs. Why? Because you probably have the rule turned off!
    Having […….

  4. Kylie BattName says:

    Увольте меня от этого….

    Секретарь-делопроизводитель If you omit braces and use Static Analysis tools it is a problem actually finding these bugs. Why? Because you probably have the rule turned…

  5. Kylie Batt says:

    Вас посетила просто великолепная идея…

    водитель If you omit braces and use Static Analysis tools it is a problem actually finding these bugs. Why? Because you probably have the rule turned off!
    Having […….

  6. Kylie Batt says:

    Какие нужные слова… супер, отличная фраза…

    менеджер, бармен If you omit braces and use Static Analysis tools it is a problem actually finding these bugs. Why? Because you probably have the rule turned off!
    Having […….