Skip to content

Instantly share code, notes, and snippets.

@kmckinley
Created July 31, 2015 02:49
Show Gist options
  • Save kmckinley/30248e217ac2e481399e to your computer and use it in GitHub Desktop.
Save kmckinley/30248e217ac2e481399e to your computer and use it in GitHub Desktop.
class FizzBuzzBang {
func start() {
var results = ""
for i in 1...100 {
results += fizzTestResult(i)
results += buzzTestResult(i)
results += bangTestResult(i)
if count(results) == 0 {
results = "\(i)"
}
println("\(i) = \(results)")
results = ""
}
}
// Divide by 7 and check if the remainder is 0
func fizzTestResult(i: Int) -> String {
if (i % 7 == 0) {
return "fizz"
} else {
return ""
}
}
// Parse index to string and test for range of 7
func buzzTestResult(i: Int) -> String {
if ("\(i)".rangeOfString("7") != nil) {
return "buzz"
} else {
return ""
}
}
// Dividing by 11 with no remainder will tell use that it's a double
func bangTestResult(i: Int) -> String {
if (i % 11 == 0) {
return "bang"
} else {
return ""
}
}
}
let fbb = FizzBuzzBang()
fbb.start()
@kmckinley
Copy link
Author

Yeah when returning out of a method I find myself being very indecisive on an explicit else or letting the method run it course. I find if a method is to long the explicit help for readability, but if it's that long then the method probably should be broke up.

@paulevans
Copy link

swift and no emoji variable names? 😞 ;)

@aterranova-bv
Copy link

@kmckinley yeah, I agree - I think it depends largely on the method as well. If it's validation-type stuff I go with the former but that's really the only rule I really stick to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment