This product is not supported for your selected Datadog site. ().
Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Metadata

ID: swift-code-style/nested-closure

Language: Unknown

Severity: Notice

Category: Code Style

Description

Closure expressions should not be nested too deeply, as excessive nesting makes code harder to read, understand, and maintain. Closures are intended to provide clear, concise logic, but when stacked inside one another, they quickly become confusing and error-prone. Extracting nested logic into separate functions keeps the codebase cleaner and easier to follow.

Non-Compliant Code Examples

//
//
//
//
//
//
//
foo(42) { (x: Int) in
    bar(x) { (x: Int) in
      foobar(x) { // Noncompliant
        print(x * 42)
      }
      print(x + 42)
    }
    print(x - 42)
}

Compliant Code Examples

func multPlus(x:Int) {
  foobar(x) {
    print(x * 42)
  }
  print(x + 42)
}

foo(42) { (x: Int) in
    bar(x, multPlus)
    print(x - 42)
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains