Sazzad Hissain Khan
Sazzad Hissain Khan

Reputation: 40156

Confusion about Bool var and func naming convention in Swift

I am developing iOS/Swift app. I am confused after seeing Bool variables and function naming convention provided by Google (https://google.github.io/swift/). Because Swift itself follows simple adverbed which is not mentioned in Googles suggestion i.e.

https://developer.apple.com/documentation/swift/array/2945493-contains

What should be the proper way of naming a func that returns Bool?

Option 1. matches(string: String) -> Bool
Option 2. isMatched(string: String) -> Bool

I prefer myself naming it as matches but my team members want to name it as isMatched.

Upvotes: 4

Views: 1248

Answers (1)

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119282

From the Swift API Design Guidelines

Uses of Boolean methods and properties should read as assertions about the receiver when the use is nonmutating, e.g. x.isEmpty, line1.intersects(line2).

So both of them are correct. The Foundation also uses both: e.g.

12.isMultiple(of: 2)
[12].contains(2)

// and more e.g.
"accept".hasPrefix("a")

The only thing matters is to be read correctly.

Upvotes: 7

Related Questions