Avoid negation in your ternary operation

This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

Metadata

ID: java-code-style/confusing-ternary

Language: Java

Severity: Notice

Category: Code Style

Description

Using a negative comparison in if expressions with an else clause can be confusing. Consider modifying your comparison by switching your if and else` block statements.

Non-Compliant Code Examples

public class Foo {
    String bar(int x, int y) {
        return (x != y) ? "diff" : "same";
    }
}

Compliant Code Examples

public class Foo {
    String bar(int x, int y) {
        return (x == y) ? "same" : "diff";
    }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis