2023-05-03 11:06:20 +02:00
== Why is this an issue?
2020-06-30 12:48:39 +02:00
The point of using closure expressions is to clearly express a succinct bit of logic. Start nesting closure expressions too deeply and you create a logic snarl that will likely snare both you and future maintainers.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-06-30 12:48:39 +02:00
With the maximum depth of 2:
2020-06-30 14:49:38 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:39 +02:00
----
foo(42) { (x: Int) in
bar(x) { (x: Int) in
foobar(x) { // Noncompliant
print(x * 42)
}
print(x + 42)
}
print(x - 42)
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:39 +02:00
----
func multPlus(x:Int) {
foobar(x) {
print(x * 42)
}
print(x + 42)
}
foo(42) { (x: Int) in
bar(x, multPlus)
print(x - 42)
}
----