- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: ruby-best-practices/no-end-blocks
Language: Ruby
Severity: Notice
Category: Best Practices
The rule of avoiding END
blocks in Ruby is important due to the nature of how such blocks are executed. Unlike at_exit
blocks, END
blocks are run whenever the program exits, regardless of whether it’s a normal termination or due to an unhandled exception. This can lead to unpredictable behavior and makes debugging more difficult.
The use of END
blocks also makes your code less readable and harder to maintain. It’s not immediately clear when or why these blocks are executed, and they can easily be overlooked when reading through the code. This can lead to unexpected side effects and bugs.
To adhere to this rule, use at_exit
blocks instead of END
blocks. This ensures that the block is only executed when the program exits normally, making your code more predictable and easier to debug. Also, consider structuring your code in a way that avoids the need for such blocks in the first place. This will make your code cleaner and easier to understand. For instance, instead of using END
to perform cleanup tasks, you can use a begin/rescue/ensure
block or make sure that resources are released as soon as they are no longer needed.
END { puts("end") }
END {puts("end")}
END {
puts("end")
}
END
{
puts("end")
}
END {}
puts("begin")
my_str = begin
a = "more "
b = "things"
a + b
end
puts(my_str)
at_exit { puts("end") }
at_exit {puts("end")}
at_exit {
puts("end")
}
at_exit
{
puts("end")
}
at_exit {}
puts("begin")
my_str = begin
a = "more "
b = "things"
a + b
end
puts(my_str)
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products