- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: ruby-best-practices/condition-safe-alignment
Language: Ruby
Severity: Notice
Category: Code Style
The rule “Wrap assignment in condition” is designed to avoid a common programming bug where a single equals sign (=
) is used in a conditional statement instead of a double equals sign (==
). This can lead to unexpected behavior because the single equals sign is used for assignment in Ruby, not for comparison.
This is important because using assignment in a conditional statement can lead to code that is hard to read and understand. It can also lead to bugs if the assignment is unintentional. The assignment will always return a truthy value unless the assigned value is nil
or false
, which may not be the expected behavior.
To avoid violating this rule, always use double equals (==
) for comparison in conditional statements. If you need to assign a value and use it in the condition, you should do the assignment outside of the conditional statement. If you must do the assignment inside the condition, wrap the assignment in parentheses to make it clear that the assignment is intentional. This improves code readability and helps prevent bugs.
if v = array.grep(/foo/)
do_something(v)
# some code
end
if (v = array.grep(/foo/))
do_something(v)
# some code
end
v = array.grep(/foo/)
if v
do_something(v)
# some code
end
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products