Swift

Swift

Made by DeepSource
Prefer using min() or max() over sorted().first or sorted().last SW-P1011
Performance
Major
Autofix

Using sorted().first or sorted().last can be inefficient, as the entire collection needs to be sorted, which is not required if we only need the minimum or maximum element. This can cause performance issues, especially when dealing with large collections.

Prefer using .first(where:) over .filter { }.first in collections SW-P1007
Performance
Major
Autofix

Using .first(where:) instead of .filter { }.first in collections, can result in cleaner and more efficient code.

Prefer using .last(where:) over .filter { }.last in collections SW-P1009
Performance
Minor
Autofix

Using .filter {}.last to get the last element of a collection is less efficient than using .last(where:). The former method first iterates through the entire collection to filter the elements and then returns the last element. This is less efficient because it allocates an intermediate buffer/allocation.

Prefer contains over range(of:) != nil and range(of:) == nil SW-P1012
Performance
Major
Autofix

Using range(of:) != nil and range(of:) == nil instead of contains can lead to slower performance and less readable code.

Prefer contains over comparing filter(where:).count to zero SW-P1001
Performance
Major

Using the filter(where:) method followed by the count property to determine if a collection contains a specific element is not as efficient as using the contains method directly.

Prefer reduce(into:_:) over reduce(_:_:) for copy-on-write types SW-P1010
Performance
Major

Using reduce(_:_:) instead of reduce(into:_:) for copy-on-write types can lead to unnecessary copies of the original collection, resulting in decreased performance and increased memory usage. This is because reduce(_:_:) creates a new copy of the collection on each iteration,

Prefer using .allSatisfy() or .contains() over reduce(true) or reduce(false) SW-P1000
Performance
Major

Using the reduce method with a boolean value to check if any or all elements in a sequence satisfy a condition is less performant than using the .allSatisfy() or .contains() method.

Prefer contains over using filter(where:).isEmpty SW-P1002
Performance
Major

Using filter(where:).isEmpty to determine if an array contains an element is a common mistake in Swift. It is an inefficient way of checking if an element exists in an array, as it requires iterating over the entire array

Prefer contains over first(where:) != nil and firstIndex(where:) != nil SW-P1003
Performance
Major

Using the contains method is a more readable and performant way to check if an element exists in an array instead of using first(where:) != nil or firstIndex(where:) != nil.

Prefer checking isEmpty over comparing collection to an empty array or dictionary literal SW-P1004
Performance
Major

Using isEmpty to check if a collection is empty is more efficient and concise than comparing the collection to an empty array or dictionary literal. Comparing collections to empty literals can lead to subtle bugs and

Prefer flatMap over map followed by reduce([], +) SW-P1008
Performance
Minor

Using map followed by reduce to transform and aggregate a collection requires two separate iterations over the collection. This can lead to performance issues when dealing with large collections. Instead, it is recommended to use flatMap to achieve the same result in a single iteration.

Prefer checking isEmpty over comparing count to zero SW-P1005
Performance
Minor

Using isEmpty is a more efficient and readable way to check if a collection is empty than comparing its count property to zero. When using count to check if a collection is empty, if the collection does not implement RandomAccessCollection, the runtime must iterate over all elements of the collection to count them before comparing the result to zero.

Prefer checking isEmpty over comparing string to an empty string literal SW-P1006
Performance
Minor
Autofix

It's better to check whether a string is empty or not using the isEmpty property rather than comparing it to an empty string literal. Why is this an issue: Comparing a string to an empty string literal is less efficient than using the isEmpty property.