- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: ruby-best-practices/parallel-assignment
Language: Ruby
Severity: Notice
Category: Best Practices
The rule “Do not use parallel assignment to define variables” is a guideline that encourages developers to avoid assigning multiple variables in a single line. This practice is often referred to as “parallel assignment” or “multiple assignment”.
Parallel assignment can lead to code that is difficult to read and understand, especially when complex expressions are involved. It can also lead to unexpected results if the order of evaluation is not clear.
To adhere to this rule, you should assign each variable individually, on its own line. This makes your code more readable and maintainable. For example, instead of writing a, b = 1, 2
, you should write a = 1
and b = 2
on separate lines. However, there are exceptions to this rule. Parallel assignment is acceptable when swapping two variables, when assigning the return values of a method, or when using the splat operator to assign multiple values to a single variable.
# Parallel assignments of literal values.
t, f, n = true, false, nil
i, f, c, r = 123, 1.0, 1i, 2.0r
s, d, i, h = 'uno', "dos", "tres#{cuatro}cinco", <<HERE
seis
HERE
s, i = :foo, :"bar#{baz}"
r1, r2 = /cuatro/, /cinco#{seis}/im
# Single assignments of literal values.
t = true
f = false
i = 123
s = "dos"
i = :"bar#{baz}"
r = /cuatro/im
# Variable swap
a = 123
b = 456
a, b = b, a
# method return
def multi
[1, 2]
end
first, second = multi
# Splat
first, *rest = [1, 2, 3]
first, *rest = 1, 2, 3
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products