11

In Swift, I am trying to figure out whether I should do

if(true)
{
    //stuff
}
else
{
    //other stuff
}

Or

if(true){
    //stuff
} else{
    //other stuff
}

I know that technically it doesn't make a difference, but I was wondering what the industry standard was, and why the standard is...the standard.

AstroCB
  • 11,800
  • 20
  • 54
  • 68
Daniel Smith
  • 794
  • 2
  • 7
  • 23
  • 3
    purely one man's opinion -- we **very strongly** insist on only the first style in every group I have. for me, that is the "new-old" style of braces handling. the second option you list - although widespread - is incredibly silly and illogical - **it's the "new math" of formatting** - nobody can explain why it was ever introduced in code, and hopefully it will go away in a few years as a bad idea. this is a hot contentious issue and it's difficult to know what is best. critically as Astro says it's ALWAYS a good idea to "do what Apple does" regarding all idiomatic issues. But not for us! – Fattie Mar 10 '15 at 02:03
  • 1
    by the way for any young people reading. "new math" was an, essentially insane, idea introduced by some governments for teaching math (around the 70s). it was incredibly ridiculous, and soon eliminated. – Fattie Mar 10 '15 at 02:06
  • Swift documentation recommends that the opening curly brace follows on the line of code prior, while objective C documentation encourages putting the opening curly brace on the line following the prior line of code. – ScottyBlades Dec 11 '17 at 05:21

2 Answers2

8

Bracket style is usually a matter of opinion.

However, in this case, there is something to go by. Apple uses the second syntax you have provided exclusively in all of its documentation, with one distinction for Swift: parentheses.

From The Swift Programming Language Guide – Control Flow:

In addition to for-in loops, Swift supports traditional C-style for loops with a condition and an incrementer...

Here’s the general form of this loop format:

for initialization; condition; increment {
    statements
}

Semicolons separate the three parts of the loop’s definition, as in C. However, unlike C, Swift doesn’t need parentheses around the entire “initialization; condition; increment” block.

In other words, you don't need parentheses around your conditional statements (in any type of loop or logic statement), and this is typically how Apple uses it in the documentation.

So, in the sample you have provided, Apple would use this style (note the spacing between the curly braces as well):

if condition {
    // Stuff
} else {
    // Other stuff
}

Some other examples from the docs:

// While loops
while condition {
    statements
}

// Do-while loops
do {
    statements
} while condition

// Switch statements
switch some value to consider {
case value 1:
    respond to value 1
case value 2,
value 3:
    respond to value 2 or 3
default:
    otherwise, do something else
}
AstroCB
  • 11,800
  • 20
  • 54
  • 68
  • Thanks! I was always taught to do it the first listed way, but I got confused when looking at apple's documentation. Do you know why apple chose this format? – Daniel Smith Mar 10 '15 at 02:07
  • @DaniM.Smith It's all a matter of style, and it only really matters in some languages (and even with those it's only with edge cases). This is the style Apple chose to use, and it obviously enforces its standard when writing documentation, but I don't know the specific reasons behind the decision. – AstroCB Mar 10 '15 at 02:08
  • Thanks for the info. – Daniel Smith Mar 10 '15 at 02:09
  • @DaniM.Smith No problem; I'm glad I could help. – AstroCB Mar 10 '15 at 02:09
  • 2
    Hi Dani, it's a great question. there is **absolutely no technical reason** to do it one way or the other. fi you worked for us, and you ever wrote code the second way, you'd be fired on the spot. conversely, apple's policy is the second way as Astro explains. note too that this applies in all programming languages. the current "absurd-ridiculous" system (i.e. the second option) is widespread in many languages. however, more and more teams are rejecting it and going back to the logical system. – Fattie Mar 10 '15 at 02:10
  • The only language I've found that it *may* make a difference for is JavaScript (see [here](http://stackoverflow.com/questions/3218756/javascript-braces-on-new-line-or-not)) due to automatic semicolon insertion, but this shouldn't be a problem in most languages. – AstroCB Mar 10 '15 at 02:13
  • The 'else' format is always *wrong*. The syntactic keyword that begins a block must be on its own line. It is not opinion; it is uniformity. – GoZoner Mar 10 '15 at 02:37
2

I have worked for different company and each of them is using different standard/coding rules.

When it comes to Apple and looking at their Swift documentation, it looks like they are using your second option.

AstroCB
  • 11,800
  • 20
  • 54
  • 68
Jérôme
  • 7,658
  • 4
  • 25
  • 34
  • 1
    If you want to code for Apple's OS & Apple's devices, it is better to follow Apple's Style. (Personal opinion) – Jeba Moses Oct 07 '17 at 11:56