So for example this code:
func trigramSimilarity(to other: Set<String>) -> Double {
let selfTrigram = trigrams()
let total = selfTrigram.union(other)
let common = selfTrigram.intersection(other)
return total.isEmpty ? 0 : Double(common.count) / Double(total.count)
}
I would hope the compiler transforms to
func trigramSimilarity(to other: Set<String>) -> Double {
let selfTrigram = trigrams()
let total = selfTrigram.union(other)
return total.isEmpty ? 0 : Double(selfTrigram.intersection(other).count) / Double(total.count)
}
By noticing that let common
is not needed if total.isEmpty
the swift compiler could allow me to write simpler code but avoid unnecessary calculations.
But is it safer to just be explicit?