이 페이지는 아직 한국어로 제공되지 않습니다. 번역 작업 중입니다. 현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.
Metadata
ID:ruby-best-practices/parallel-assignment
Language: Ruby
Severity: Notice
Category: Best Practices
Description
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.
Non-Compliant Code Examples
# Parallel assignments of literal values.t,f,n=true,false,nili,f,c,r=123,1.0,1i,2.0rs,d,i,h='uno',"dos","tres#{cuatro}cinco",<<HERE
seisHEREs,i=:foo,:"bar#{baz}"r1,r2=/cuatro/,/cinco#{seis}/im
Compliant Code Examples
# Single assignments of literal values.t=truef=falsei=123s="dos"i=:"bar#{baz}"r=/cuatro/im# Variable swapa=123b=456a,b=b,a# method returndefmulti[1,2]endfirst,second=multi# Splatfirst,*rest=[1,2,3]first,*rest=1,2,3
원활한 통합. Datadog Code Security 사용해 보기
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드 분석해 보기
이 규칙의 사용법
1
2
rulesets:- ruby-best-practices # Rules to enforce Ruby best practices.
리포지토리 루트에서 위의 콘텐츠를 사용해 static-analysis.datadog.yml 생성
Datadog의 무료 IDE Plugins를 사용하거나 Code Security 스캔을 CI 파이프라인에 추가