Skip to content

Latest commit

 

History

History
38 lines (19 loc) · 683 Bytes

contains_over_first_not_nil.md

File metadata and controls

38 lines (19 loc) · 683 Bytes

Pattern: Use of first(where:) != nil

Issue: -

Description

Prefer contains over first(where:) != nil.

Examples of correct code:

let first = myList.first(where: { $0 % 2 == 0 })


let first = myList.first { $0 % 2 == 0 }

Examples of incorrect code:

myList.first { $0 % 2 == 0 } != nil


myList.first(where: { $0 % 2 == 0 }) != nil


myList.map { $0 + 1 }.first(where: { $0 % 2 == 0 }) != nil


myList.first(where: someFunction) != nil


myList.map { $0 + 1 }.first { $0 % 2 == 0 } != nil

Further Reading