- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
",t};e.buildCustomizationMenuUi=t;function n(e){let t='
",t}function s(e){let n=e.filter.currentValue||e.filter.defaultValue,t='${e.filter.label}
`,e.filter.options.forEach(s=>{let o=s.id===n;t+=``}),t+="${e.filter.label}
`,t+=`ID: swift-code-style/closure-max-lines
Language: Unknown
Severity: Notice
Category: Code Style
Closures are designed to be a concise way of injecting behavior without defining a separate function. However, when a closure grows beyond a few lines, it becomes harder to read and maintain. Large closures reduce clarity and make the code less adaptable. To keep the source clean and understandable, closures should remain short, and more complex logic should be extracted into dedicated functions.
max-lines
: Maximum number of lines allowed in a closure. Default: 40.
// A list of numbers to process.
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// NON-COMPLIANT: The closure passed to `forEach` is too long and complex.
// It mixes data transformation, filtering, and printing, harming readability.
numbers.forEach { number in
// 1. First, let's double the number
let doubled = number * 2
print("Processing \(number)...")
// 2. Check if the doubled number is a multiple of four
if doubled.isMultiple(of: 4) {
// 3. Now, let's perform a more complex check
// Imagine some business logic here
let isSpecialCase = (doubled + number) % 3 == 0
if isSpecialCase {
print("Found a special case for \(number)!")
}
print("Final doubled value is \(doubled)")
} else {
print("\(doubled) is not a multiple of 4.")
}
}
import Foundation
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
/// Processes a single number with all the required business logic.
/// By moving the logic here, we make it testable and reusable.
func processNumber(_ number: Int) {
let doubled = number * 2
print("Processing \(number)...")
if doubled.isMultiple(of: 4) {
let isSpecialCase = (doubled + number) % 3 == 0
if isSpecialCase {
print("Found a special case for \(number)!")
}
print("Final doubled value is \(doubled)")
} else {
print("\(doubled) is not a multiple of 4.")
}
}
// COMPLIANT: The closure is now a simple, one-line call
// to a well-named function. The code is readable and maintainable.
numbers.forEach(processNumber)
// An alternative compliant example using a trailing closure syntax:
numbers.forEach { number in
processNumber(number)
}