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

원활한 통합. Datadog Code Security를 경험해 보세요