Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,159 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!