- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: javascript-best-practices/no-useless-jumps
Language: JavaScript
Severity: Notice
Category: Best Practices
Jump statements like return
, break
, and continue
are used to control the flow of the program, and while they can be extremely useful, unnecessary use of these statements can lead to code that is harder to read and understand.
This rule is important because unnecessary jump statements can clutter your code, make it less readable, and potentially introduce bugs. To adhere to this rule, you should only use jump statements where they are absolutely necessary. For instance, a return
statement should only be used when you want to exit a function and return a value, a break
statement should only be used to exit a loop or a switch statement, and a continue
statement should only be used to skip the current iteration of a loop.
function badReturn() {
doComputation();
// Unnecessary
return;
}
function badLoops() {
const arr = [1, 2, 3];
for (const elem of arr) {
console.log(elem);
// Unnecessary
continue;
}
while (true) {
console.log(`1: ${arr[0]}`)
console.log(`2: ${arr[1]}`)
console.log(`3: ${arr[2]}`)
break;
}
}
function goodReturn() {
doComputation();
return 2;
}
function inSwitch() {
const val = goodReturn();
switch (val) {
case 1:
return;
case 2:
return;
default:
break;
}
doComputation();
}
function goodLoops() {
const arr = [1, 2, 3, 4, 5];
for (int = 0; i < arr.length, i++) {
console.log(arr[i]);
if (i === 2) {
break;
}
}
int i = 0;
while (true) {
console.log(`${i+1}: ${arr[i]}`);
i++;
if (i === 3) {
break;
}
}
}