Skip to content

Files

Latest commit

 

History

History
23 lines (15 loc) · 758 Bytes

DoubleNegativeLambda.md

File metadata and controls

23 lines (15 loc) · 758 Bytes

Pattern: Use of double negative lambda

Issue: -

Description

Detects negation in lambda blocks where the function name is also in the negative (like takeUnless). A double negative is harder to read than a positive. In particular, if there are multiple conditions with && etc. inside the lambda, then the reader may need to unpack these using DeMorgan's laws. Consider rewriting the lambda to use a positive version of the function (like takeIf).

Example of incorrect code:

fun Int.evenOrNull() = takeUnless { it % 2 != 0 }

Example of correct code:

fun Int.evenOrNull() = takeIf { it % 2 == 0 }

Further Reading