Categories
iOS Development

Clean Code for Swift – Chapter 2: Meaningful Names

Say what you mean, and mean what you say.

Chapter 2 is all about naming! Names of variables, functions, classes, objects, protocols (interfaces), everything!

Why is naming so important?

Everything in software has a name, from the iterator variable in a for loop to the package you’re importing into your project. The name tells you what is happening and gives you an idea of its context, or it’s supposed to.

Imagine if the variables in a program were only named a, b, c, d, etc. You would have no idea what’s going on! Names are supposed to act like sign posts. When hiking you use sign posts to tell you where to go. The same should go for the names in your program.

You read code way more often than you write new code. To make your code easier to write, you must make it easier to read.

Meaningful Names

Meaningful names have these characteristics:

Avoid Disinformation

Uncle Bob says a variable named accountsList can be misleading because it’s not an actual List data structure, in Java. In Swift, we don’t have a List, but also there’s not a reason to encode the container type in the name. More on this later. This variable should just be called accounts.

Don’t Use Puns

While you may think you’re being funny, this abstracts the meaning and can easily confuse newcomers in the code base.

Say what you mean. Mean what you say.

Clean Code, Chapter 2

Use Searchable Names

If you have a photo app named Awesome Photos and you’ve prefixed many classes and objects with AP, it’ll be difficult to search what you’re looking for. When typing a new instance of an object and you begin typing AP, now autocorrect is going to suggest many different things, rather than if you’ve chosen a more distinctive searchable name.

The name also depends on where you’re using it. Uncle Bob’s rule is:

The length of a name should correspond to the size of its scope

Clean Code, Chapter 2

This means the larger the scope of the variable, the longer the name, and vise versa. That’s why it’s okay to use i, j, k, as variable names in for loops (small scope), but you definitely shouldn’t use them as a class variable (larger scope).

Class Names

Class names should be a noun or noun phrase. It should NOT be a verb.

Good class names: Customer, Manager, Processor, Data, Information, WikiPage.

Method (Function) Names

Function names should be a verb, or verb phrase.

Good function names: postPayment, deletePage, save.

Use Pronounceable Names

This is one I didn’t think about, but it makes sense. You’ll be discussing code with co-workers, if you can’t pronounce the name, that conversation is going to be more difficult.

Don’t Use Hungarian Notation

Hungarian notation is when you add the type to a name to identify it. This can obfuscate the object and make it confusing to use.

Names like: fpPrice, and bBusy, fnFunctionName are confusing and don’t make sense. fp for floating point, b for boolean, and fn to denote a function.

If you don’t know that’s the convention this is confusing. Most importantly though, Swift is strongly typed. A variable declared as a String can’t be used as an Int, inherently without typecasting. There’s no need to prefix names with a type.

The better option is to use a better name: price, isBusy, and goodFunctionName.

Conclusion

Clarity, clarity, clarity. Make the meaning of the name clear and easy to understand. Say what you mean, and mean what you say!