This product is not supported for your selected Datadog site. ().

Metadata

ID: apex-code-style/mergeable-if

Language: Apex

Severity: Notice

Category: Code Style

Description

This rule identifies nested if statements that can be simplified by merging their conditions into a single if statement. Encapsulated if statements that do not contain else blocks or additional logic should be combined using logical operators such as && to improve readability and reduce unnecessary code nesting.

To avoid violations of this rule, developers should look for opportunities to merge nested if conditions when there are no else blocks or separate logic involved. For example, instead of writing if (something) { if (somethingElse) { ... } }, it is better to write if (something && somethingElse) { ... }. This practice helps keep code clean and straightforward.

Non-Compliant Code Examples

class MyClass {
    public void myFunction(){
        if (something)
        {
            /*
             something
            */
            if (somethingElse)
            {

            }
            /*
             something else
            */
        }
    }
}
class MyClass {
    public void myFunction(){
        if (something)
        {
            // something
            if (somethingElse)
            {

            }
            // something else
        }
    }
}
class MyClass {
    public void myFunction(){
        if (something)
        {
            if (somethingElse)
            {

            }
        }
    }
}

Compliant Code Examples

class MyClass {
    public void myFunction(){
        
        if (something)
        {
            foo = bar;
            if (somethingElse)
            {

            }
        }
        
    }
}
class MyClass {
    public void myFunction(){
        if (something)
        {
            if (somethingElse)
            {

            }
            foo = bar;
        }
        
    }
}
class MyClass {
    public void myFunction(){
        if (something)
        {
            if (somethingElse)
            {

            } else {
                
            }
        }
    }
}
class MyClass {
    public void myFunction(){
        if (something)
        {
            if (somethingElse)
            {

            }
        } else {
            
        }
    }
}
class MyClass {
    public void myFunction(){
        if (something && somethingElse)
        {
        }
    }
}
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 Security