Created
July 31, 2015 02:49
-
-
Save kmckinley/30248e217ac2e481399e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Also, well done on splitting out the tests into their own functions. Makes unit testing and bug fixing much easier.
👍
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.
swift and no emoji variable names? 😞 ;)
@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
Yep :)
Up to personal preference of course, but you could save yourself a few keystrokes by writing
like this instead:
That said, I actually prefer the former I think - more explicit.