Este producto no es compatible con el sitio Datadog seleccionado. ().
Esta página aún no está disponible en español. Estamos trabajando en su traducción.
Si tienes alguna pregunta o comentario sobre nuestro actual proyecto de traducción, no dudes en ponerte en contacto con nosotros.

Metadata

ID: swift-code-style/nested-closure

Language: Swift

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

import SwiftUI

struct AlbumDetail: View {
	var album: Album

	var body: some View {
		List(album.songs) { song in 
			HStack {
				Image(album.cover)
				VStack(alignment: .leading) {
					Text(song.title)
					Text(song.artist.name)
						.foregroundStyle(.secondary)

          VStack(alignment: .leading) {
            Text(song.title)
            Text(song.artist.name)
              .foregroundStyle(.secondary)
            VStack(alignment: .leading) {
              Text(song.title)
              Text(song.artist.name)
                .foregroundStyle(.secondary)
            }
          }
				}
			}
		}
	}
}

Compliant Code Examples

import SwiftUI

struct AlbumDetail: View {
	var album: Album

	var body: some View {
		List(album.songs) { song in 
			HStack {
				Image(album.cover)
				VStack(alignment: .leading) {
					Text(song.title)
					Text(song.artist.name)
						.foregroundStyle(.secondary)
				}
			}
		}
	}
}
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

Integraciones sin problemas. Prueba Datadog Code Security