Tagged: code smell

Antipatern: Negatively named boolean method

There is an inspection Java | Data flow issues | Negatively named boolean variable

Reports negatively named variables, for example ‘disabled’, ‘hidden’, ‘isNotChanged’. It is usually more clear to invert the boolean value and remove the negation from the name.

But no any inspections for methods.
For example the Apache Commons Lang library’s StringUtils class has two methods: isBlank() and isNotBlank(). The second one has a negativation «Not» and works like !isBlank().
This is antipattern, because we can use automatic refactoring from intellij to invert condition that calling this method.
I mean this refactoring Invert Boolean and Invert If-Else Statement intention.
For examle we can invert a conditionwith isBlank():
And result of this refactoringwill be:

But if you invert a condition with isNotBlank() you willl get a double negativation smell:
Now it’s readed as «not not blank» (i.e. blank).

Another one solution is to use method names that hasn’t inversion, for example hasText()


Please, avoid this in you code
We will create an Inspection for IntelliJ Idea that will check this cases