Fast industry standard for braces

In Swift, I'm trying to figure out if I should do

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

Or

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

I know that technically it doesn’t matter, but I was wondering what the industry standard is and why the standard ... standard.

+6
source share
2 answers

Bracket style is usually a matter of opinion.

However, in this case there is something to be done. Apple uses the second syntax that you provided exclusively in all of your documentation, with one difference for Swift: parentheses.

From Swift Programming Language Guide - Control Flow :

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

There is a general form of this loop format:

 for initialization; condition; increment { statements } 

Semicolons separate the three parts of the loop definition, as in C. However, unlike C, Swift does not need parentheses throughout the "initialization, condition; increment" block.

In other words, you don't need parentheses around your conditional statements (in any type of loop or logical statement), and this is usually used by Apple in the documentation.

So, in the example you provided, Apple will use this style (note the gap between curly braces):

 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 } 
+6
source

I worked in different companies, and each of them uses different standards / coding rules.

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

+2
source

Source: https://habr.com/ru/post/983557/


All Articles