Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 760
Sazzad Hissain Khan Avatar asked Oct 26 '25 07:10

Sazzad Hissain Khan


1 Answers

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.

like image 198
Mojtaba Hosseini Avatar answered Oct 28 '25 19:10

Mojtaba Hosseini