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 2.0 Collections and Control Flow Control Flow With Conditional Statements The Power of Switching

Eduardo Calvachi
Eduardo Calvachi
6,049 Points

Indentation for switch statements

What is the convention for indentation of the switch statements?

First way:

switch randomTemperature {
    case 0..<32 : print("Freezing Cold, don't leave the house")
    case 32..<45: print("Jacket son")
    case 45..<70: print("Light Sweater")
    case 70...100: print("T-shirt son, enjoy the weather")
    default: "Alien Temperature, my face is melting"
}

Second way:

switch randomTemperature {
case 0..<32 : print("Freezing Cold, don't leave the house")
case 32..<45: print("Jacket son")
case 45..<70: print("Light Sweater")
case 70...100: print("T-shirt son, enjoy the weather")
default: "Alien Temperature, my face is melting"
}

Thanks

1 Answer

Nathan Tallack
Nathan Tallack
22,160 Points

It is customary to indent everything within a brace so that you don't forget to close your brace. So your first example would be following that custom.

That being said, consistency is most important. So if you use your second option make sure you do it all through your code and you should be ok. A reader would quickly work out that you are not indenting your switch statements and no harm no foul. ;)

Eduardo Calvachi
Eduardo Calvachi
6,049 Points

Alright, I also like indenting so I'll stick to that convention, thanks Nathan!