Inverted boolean logic is hard to read and should be avoided

This product is not supported for your selected Datadog site. ().
이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

Metadata

ID: apex-code-style/inverted-boolean-logic

Language: Apex

Severity: Notice

Category: Best Practices

Description

This rule highlights the importance of avoiding inverted boolean logic, such as using negations with equality or relational expressions. Inverted boolean logic often makes code harder to read and understand because it requires extra mental effort to parse expressions like !(foo == 42) instead of the more straightforward foo != 42. Clear and direct boolean expressions improve code readability and maintainability.

To comply with this rule, prefer writing boolean expressions without negations around comparisons. For example, use foo != 42 instead of !(foo == 42), and bar <= 51 instead of !(bar > 51). Adopting this practice leads to cleaner, more intuitive code that aligns with common coding standards and best practices in Apex development.

Non-Compliant Code Examples

if (!(foo == 42)) {
}


Boolean b = !(bar > 51);

Compliant Code Examples

if (foo != 42) {
}


Boolean b = (bar <= 51);
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

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