Skip to main content
deleted 933 characters in body
Source Link
syb0rg
  • 21.9k
  • 10
  • 113
  • 193
  • 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.

Other languages also have this option, such as Ruby or Scala. Quoting from Elements of Ruby Style:

Ruby allows you to leave out parenthesis, in general, resist this temptation.

Parenthesis make the code easier to follow. General Ruby style is to use them, except in the following cases:

  • Always leave out empty parentheses
  • The parentheses can be left out of a single command that is surrounded by ERb delimiters -- the ERb markers make sure the code is still readable
  • A line that is a single command and a single simple argument can be written without the parenthesis. Personally, I find that I do this less and less, but it's still perfectly readable. I tend not to like single lines in regular ruby code that have multiple arguments and no parentheses.
  • A lot of Ruby-based Domain Specific Languages (such as Rake) don't use parenthesis to preserve a more natural language feel to their statements.

I feel like these "style rules" are very transferable to Swift as well, and should be applied in a similar fashion.

-

  • The parenthesis in if statements are optional in Swift. Should that be a thing, or should we stick with them?

Other languages also have this option, such as Ruby or Scala. Quoting from Elements of Ruby Style:

Ruby allows you to leave out parenthesis, in general, resist this temptation.

Parenthesis make the code easier to follow. General Ruby style is to use them, except in the following cases:

  • Always leave out empty parentheses
  • The parentheses can be left out of a single command that is surrounded by ERb delimiters -- the ERb markers make sure the code is still readable
  • A line that is a single command and a single simple argument can be written without the parenthesis. Personally, I find that I do this less and less, but it's still perfectly readable. I tend not to like single lines in regular ruby code that have multiple arguments and no parentheses.
  • A lot of Ruby-based Domain Specific Languages (such as Rake) don't use parenthesis to preserve a more natural language feel to their statements.

I feel like these "style rules" are very transferable to Swift as well, and should be applied in a similar fashion.

-

  • 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.

added 3 characters in body
Source Link
syb0rg
  • 21.9k
  • 10
  • 113
  • 193
  • 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 "Fizz""FizzBuzz"
         case (_0, 0_):
             return "Buzz""Fizz"
         case (0_, 0):
             return "FizzBuzz""Buzz"
         default:
             return "\(i)"
     }
    
  • 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, _):
             return "Fizz"
         case (_, 0):
             return "Buzz"
         case (0, 0):
             return "FizzBuzz"
         default:
             return "\(i)"
     }
    
  • 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)"
     }
    
added 2438 characters in body
Source Link
syb0rg
  • 21.9k
  • 10
  • 113
  • 193

Answers to your questions

  • The parenthesis in if statements are optional in Swift. Should that be a thing, or should we stick with them?

Other languages also have this option, such as Ruby or Scala. Quoting from Elements of Ruby Style:

Ruby allows you to leave out parenthesis, in general, resist this temptation.

Parenthesis make the code easier to follow. General Ruby style is to use them, except in the following cases:

  • Always leave out empty parentheses
  • The parentheses can be left out of a single command that is surrounded by ERb delimiters -- the ERb markers make sure the code is still readable
  • A line that is a single command and a single simple argument can be written without the parenthesis. Personally, I find that I do this less and less, but it's still perfectly readable. I tend not to like single lines in regular ruby code that have multiple arguments and no parentheses.
  • A lot of Ruby-based Domain Specific Languages (such as Rake) don't use parenthesis to preserve a more natural language feel to their statements.

I feel like these "style rules" are very transferable to Swift as well, and should be applied in a similar fashion.

-

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.


Answers to your questions

  • The parenthesis in if statements are optional in Swift. Should that be a thing, or should we stick with them?

Other languages also have this option, such as Ruby or Scala. Quoting from Elements of Ruby Style:

Ruby allows you to leave out parenthesis, in general, resist this temptation.

Parenthesis make the code easier to follow. General Ruby style is to use them, except in the following cases:

  • Always leave out empty parentheses
  • The parentheses can be left out of a single command that is surrounded by ERb delimiters -- the ERb markers make sure the code is still readable
  • A line that is a single command and a single simple argument can be written without the parenthesis. Personally, I find that I do this less and less, but it's still perfectly readable. I tend not to like single lines in regular ruby code that have multiple arguments and no parentheses.
  • A lot of Ruby-based Domain Specific Languages (such as Rake) don't use parenthesis to preserve a more natural language feel to their statements.

I feel like these "style rules" are very transferable to Swift as well, and should be applied in a similar fashion.

-

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.

Source Link
syb0rg
  • 21.9k
  • 10
  • 113
  • 193
Loading