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
Corey Schroeder
12,281 PointsSwift Variable / Constant Questions
Hello, I'm new to Swift and have a few basic questions -
A) Constants - In my experience with other languages, the standard naming convention is all upper case. In swift, it seems like the best practice is to follow the variable naming convention...
I understand that you can easily option-click the name of a variable (or constant) to determine if it is in fact a variable or constant.... But, why? Why not use the standard all caps naming convention for constants? When you are scanning through lines and lines of code, its easy to overlook a constant as being a standard variable because they are named the same way.
Is there a specific reason why all caps isn't the standard in swift?
B) The Underscore - I've noticed that XCode will throw an error from time to time on variables or constants that have not yet been used in other areas of my program. It prompts me to add an underscore to the beginning of the variable name. Can someone explain to me what exactly this means / what it does to my code?
I appreciate your help!
1 Answer
Greg Kaleka
39,021 PointsHi Corey,
A) I don't know if I can articulate why, exactly, but constants are different in Swift. They're used the same way variables are except for the fact that they don't change. To me it's always felt like constants in other languages were different. Often they're global, or at least higher-scope, whereas in Swift, they are whatever scope you wish, just like variables. Like many things in Swift, they are emphasized for safety. If you don't have a good reason to want to change something, make it a constant instead of a variable, and let the compiler yell at you later if you do change it. Typically you won't question if a property is a variable or a constant, because you'll need to change the variables, and you won't need to change the constants.
Anyway, because it's used so much the same way as a variable, the decision was to not change the naming convention. I have never found this to be problematic, personally.
B) This warning (note it's not actually an error) is meant to save memory where you don't need to allocate an instance of a class. An example would be in an if let statement, perhaps most commonly one in which you are checking multiple optionals for nil values, but you don't need to use all of them. See below:
if let _ = someDictionary["success marker"], importantData = someDictionary["data"] {
// do some stuff with the data
}
Hope that helps!
Cheers
-Greg
Corey Schroeder
12,281 PointsCorey Schroeder
12,281 PointsThanks for taking the time to answer - much appreciated!