Can the Java – checkstyle module “needbraces” use nested if / else blocks?

We are using checkstyle to enforce our style standards One of the style rules we chose to include is the needbraces module

Needbraces specifies that each block type statement (for example, if, else, for) must have an open brace and a close brace But as far as I know, it doesn't work exactly right

This example will trigger a checkstyle error

if (true)
    {
        System.out.println("20");   
    }
    else
        System.out.println("30");

Because else cases have no braces However, the next example cannot trigger a checkstyle error

if (true)
    {
        System.out.println("20");   
    }
    else
        if (true)
        {
            System.out.println("30");
        }

This should fail because curly braces are missing in the else case, but checkstyle lets it pass After examining the document carefully, I couldn't find any reason why it couldn't work properly

So... Can the checkstyle module "needbraces" use nested if / else blocks? Any ideas?

The answer to this question is another question: are there rules to mark the above bad codes as violations?

Solution

I believe this is an exception, because although the format is strange, what you have is "other ifs" In this case, it should not force you to put parentheses around "if", because you will eventually get "... Else {if {...}}

Your code should be formatted:

if (true)
{
    System.out.println("20");   
}
else if (true)
{
    System.out.println("30");
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>