patternswiftMajor
Is this FizzBuzz Swift-y?
Viewed 0 times
thisswiftfizzbuzz
Problem
for (var i = 1; i <= 100; ++i) {
var fizzBuzz = ""
if i % 3 == 0 {
fizzBuzz += "Fizz"
}
if i % 5 == 0 {
fizzBuzz += "Buzz"
}
if fizzBuzz == "" {
fizzBuzz += "\(i)"
}
println(fizzBuzz)
}I don't really like comparing strings with
==, but apparently, that's how you do it in Swift (and there's not another option). The parenthesis in
if statements are optional in Swift. Should that be a thing, or should we stick with them? The curly-braces were optional in Objective-C (and lots of programming languages) but they're not in Swift. Despite their former optionality, I never thought it was a good idea to not use them--is it a good idea to not use parenthesis here?Not explicitly declaring the type of variable is now a thing in Swift (although the variable still has an explicit type, it's just implicitly determined). Is it okay to let the type be implicitly determined, or should we stick with explicitly declaring the type?
Solution
Is this FizzBuzz Swift-y?
Kinda, but it could be a lot better. Here's what I would do to fix it:
-
Extrapolate this code into a method, then call the method from the
-
There is a handy Swift feature called "Tuples". Tuples are groupings of values. We can use them to represent our results from the modulo operation.
-
Now that we are using a Tuple to represent the result, we can easily use a
You might be wondering what that underscore is. That
-
Your
Answers to your questions
-
The parenthesis in if statements are optional in Swift. Should that be
a thing, or should we stick with them?
This is a very style-oriented question. Style differs from person to person, and is very organic. Since no "style rules" have been put in place, do whatever you are most comfortable with.
-
The curly-braces were optional in Objective-C (and lots of programming
languages) but they're not in Swift.
This was implemented in order to prevent simplified
Swift's forced usage of braces eliminated the execution of
-
Is it okay to let the type be implicitly determined, or should we
stick with explicitly declaring the type?
Whether or not you include the explicit type is a matter of taste. In some contexts it might make your code more readable. However, this is usually not the case; and since the compiler will almost never assign the wrong type to your variable, I usually leave the type to be implicitly determined. Whatever way you decide will not affect speed/efficiency of the code, so that is not a factor.
Kinda, but it could be a lot better. Here's what I would do to fix it:
-
Extrapolate this code into a method, then call the method from the
for loop.func fizzbuzz(i: Int) -> String
{
// ...
}-
There is a handy Swift feature called "Tuples". Tuples are groupings of values. We can use them to represent our results from the modulo operation.
let result = (i % 3, i % 5)-
Now that we are using a Tuple to represent the result, we can easily use a
switch to perform the necessary actions.switch result
{
case (0, 0):
return "FizzBuzz"
case (0, _):
return "Fizz"
case (_, 0):
return "Buzz"
default:
return "\(i)"
}You might be wondering what that underscore is. That
_ is used to indicate a discarded value that we don't really care about, the value in the tuple that doesn't really matter to us in the evaluation.-
Your
for loop isn't very "Swift-y". Here's how I would write is so that it calls the function 100 times.for number in 1...100
{
println(fizzbuzz(number))
}Answers to your questions
-
The parenthesis in if statements are optional in Swift. Should that be
a thing, or should we stick with them?
This is a very style-oriented question. Style differs from person to person, and is very organic. Since no "style rules" have been put in place, do whatever you are most comfortable with.
-
The curly-braces were optional in Objective-C (and lots of programming
languages) but they're not in Swift.
This was implemented in order to prevent simplified
if conditional statements and the class of bugs that associated with them. For example:if (someCondition)
doThisForSomeCondition()
doThisAsWell()
if (someOtherCondition)
// ...Swift's forced usage of braces eliminated the execution of
doThisAsWell() outside of the someCondition conditional statement.-
Is it okay to let the type be implicitly determined, or should we
stick with explicitly declaring the type?
Whether or not you include the explicit type is a matter of taste. In some contexts it might make your code more readable. However, this is usually not the case; and since the compiler will almost never assign the wrong type to your variable, I usually leave the type to be implicitly determined. Whatever way you decide will not affect speed/efficiency of the code, so that is not a factor.
Code Snippets
func fizzbuzz(i: Int) -> String
{
// ...
}let result = (i % 3, i % 5)switch result
{
case (0, 0):
return "FizzBuzz"
case (0, _):
return "Fizz"
case (_, 0):
return "Buzz"
default:
return "\(i)"
}for number in 1...100
{
println(fizzbuzz(number))
}if (someCondition)
doThisForSomeCondition()
doThisAsWell()
if (someOtherCondition)
// ...Context
StackExchange Code Review Q#56805, answer score: 45
Revisions (0)
No revisions yet.