Coding in style in 60 seconds - capitalization

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

To view or add a comment, sign in

More articles by Dragos Ionel

  • AIGeeks 2019 in review, plans for 2020

    As we’re wrapping 2019 up, we have high hopes for how AI will improve our lives in 2020. This year, we organized and…

    2 Comments
  • How can AIGeeks help you?

    As you know AIGeeks is a Non Profit trying to improve our world around us using AI. Other than organizing the meetup…

  • Be cautious with the ternary operator

    The operators in a programming language, in general, and in Swift, in particular, are of three types: Unary - that…

    2 Comments
  • Use long names when writting your code

    Unfortunately, we have been used to the fact that a name has nothing to do with the object it names. John, Mary, and…

  • Write tests

    Welcome to another post about how to improve our code. Today we are trying to prove that the quality of the code…

    4 Comments
  • How to name a function - Coding in Style - episode 11

    Welcome to Coding in style, a series of short videos about how to improve our code. This is Dragos and you are watching…

  • Reducing the cyclomatic complexity - Coding in style - Episode 9

    Welcome to Coding in style, a series of short videos about how to improve our coding. This is Dragos and you are…

  • To comment or not to comment

    Welcome to Coding in Style, a series of short videos about how we can improve our code. This is the episode 6 where we…

    2 Comments
  • Coding in style in 60 seconds - Comments

    Welcome to Coding in Style in 60 seconds. This is Dragos and you are watching a series of short videos about how we can…

  • Get smart by being stupid

    Do you know why programming is the best job ever? From the thousands and thousands of possible jobs out there…

Explore content categories