Este producto no es compatible con el sitio Datadog seleccionado. ().
Esta página aún no está disponible en español. Estamos trabajando en su traducción.
Si tienes alguna pregunta o comentario sobre nuestro actual proyecto de traducción, no dudes en ponerte en contacto con nosotros.

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

Integraciones sin problemas. Prueba Datadog Code Security