Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Collections and Control Flow Control Flow With Conditional Statements Switch Statements

Knut Bendik Manger Breistein
Knut Bendik Manger Breistein
1,161 Points

Code placement

Talking about Switch statements and their cases: @ 07.40 Pasan says: “We’ll indent these correctly” and he moves the cases into a different placement than the default (he moves it to the start of the line instead of keeping it at the swift-generated “two tabs into the line”-placement) Any reason why?

I may not know too much about this yet, but I have relied upon using auto placement as a sort of a “tell tale” for knowing whether or not I’m doing right or wrong in the code (and this kinda contradicts this).

2 Answers

Charles Kenney
Charles Kenney
15,604 Points

Negatively indenting switch statements has always been a convention, but many people do not follow it. In Xcode, anytime you open a set of curl braces and press enter, the page is automatically indented. But upon typing out your case clause, everything should shift back one tab. It is often argued that the case statement should act as a label in control flow and not indenting it makes it more readable.

Your indention in a switch-case statement will not effect the functionality of your code, but it is always best practice to follow Apple's implementation.

When in doubt, check the docs and see how Apple is writing it in their code examples. Swift 3.1 Docs: Control Flow

More examples of this convention in practice:

C# Switch Statement Conventions Java Switch Statement Conventions

Hope this helps,

-Charles

Knut Bendik Manger Breistein
Knut Bendik Manger Breistein
1,161 Points

@Charles Kenney Thanks for your answer Charles. That clarifies a few things:) Will check out documentation as well!

  • Knut B