Tuesday, August 23, 2016
Capitalization (Coding in style in 60 seconds)
Welcome to “Coding in Style in 60 seconds”, a collection of short videos about how we can improve our code in simple steps. This is Dragos, the speaker for this video.
Do you have any idea what is the first reason for which the code is discarded? Of course, other than the fact that it is not working. Eh? Because it is ugly!!!
Who likes to keep ugly things around? We like to live in a beautiful house, drive a beautiful car and marry a beautiful woman or a handsome guy. But I digress.
One of the fundamental pieces of the code is a variable. So we need to make it beautiful.
To start with, decide on the capitalization. For example, in Swift, the most common one is lower camel case. That is: start the name of the variable with a lower letter and capitalize each of the next words in the name if any.
Let’s take a look at few examples.
var clientName = "john"
var cityOfBirth = "Toronto"
var numberOfSecondsInAMinute = 60
In this example, all the variables are nicely formatted, with first word starting with a lower letter and the next ones with a capital.
Here is another example, not that good this time.
var first_name = "ugly name"
var FirstName = "this is a class name"
var firstNAME = "another ugly name"
The first variable has an underscore, the second one has both words capitalized and the last is a mess.
Let’s talk a bit about constants. A constant is a variable that does not vary so we can use the same rules. With just one addition. To differentiate a constant from a variable, we can start its name with a k.
Here is a good example.
let kClientName = "john"
let kCityOfBirth = "Toronto"
let kNumberOfSecondsInAMinute = 60
And here is a messy one.
let CLIENT_NAME = "john"
let CITY_OF_BIRTH = "Toronto"
let NUMBER_OF_SECONDS_IN_A_MINUTE = 60
Posted by Dragos