2

I am using checkstyles and I'd like to define the rule like where it displays a warning in case someone uses not condition under if ... else ... block. For example:

if (!employeeNotExist) { 
  // code
} else {
  // code
}

Reason being it's good to have only positive condition for readability

Is there ready made module for this under checkstyle or some custom checkstyle rule needs to be written for this?

2 Answers 2

1

I don't think this check is defined in checkstyle itself, but you can always use one of the extension plugins i.e. https://github.com/sevntu-checkstyle/sevntu.checkstyle, where you can find what you're after here: here.

Sign up to request clarification or add additional context in comments.

1 Comment

i am using eclipse-cs plugin
1

Checkstyle cannot do this out of the box. So if using a third-party library is no option for you, you will have to write a custom check. If it's any comfort: This would be quite a simple case of custom check.

The best you can do out of the box is to configure a DescendantToken check like this (works in Eclipse-CS; DescendantToken checks are complex low-level checks which require understanding of the AST to configure, please see the linked documentation for more information):

<module name="DescendantToken">
    <property name="tokens" value="LITERAL_IF"/>
    <property name="limitedTokens" value="LNOT"/>
    <property name="minimumDepth" value="2"/>
    <property name="maximumDepth" value="2"/>
    <property name="maximumNumber" value="0"/>
    <property name="maximumMessage" value="Consider changing this to a
            positive condition for better readability."/>
</module>

This approach is somewhat crude, as it will not detect if there is an else block, and it also flags method calls such as if (!map.isEmpty()). But it covers your if (!employeeNotExist) case. It will also flag conditions like !(a || b), but those can be rewritten to !a && !b.

So, all in all, it's far from perfect, but this is the closest you're going to get with out-of-the-box Checkstyle.

5 Comments

Thanks Thomas. What do you mean by false positives ?
False positives are cases where the check reports an issue, but in reality everything is good. In contrast, a false negative is a case where the check misses an issue.
As i stated in OP, i want to flag only if else blocks where if contains not operator( i dont want to flag where if without else contains not operator)
Also i did not get what minimumDepth, maximumDepth, minimumNumber, maximumNumber means ? It would be helpful if you explain the term giving some example. Thanks in advance.
Right, this won't consider any else blocks. I updated the answer to clarify. My answer shows what's possible without a custom check. I am aware that it's not your desired solution, but it is all you can do without writing your own check. This may still help to provide a basis for a manual inspection run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.