Following the last post, we continue our exploration of the book Clean Code by Robert C. Martin. This time going over the second chapter - titled Meaningful Names.
But before we do that, I wanted to mention that the very act of reading this book has already begun to benefit my code. Not so much in the application of the lessons, but rather, because clean code is on my mind, I have an increased awareness on my own - and that makes me happy.
Ok, so… names. Naming things. Much of our job as programmers is to give things names. Variables, functions, classes, programs, files…etc. Therefore, doing it well is..well worth our time.
I’m going to do this post a little differently and simply list a handful of key points I thought were interesting or useful:
- Don’t use names like
strNamesince the string type is implied.
- If it requires a comment, it’s not a good name.
- Use meaninful distinctions (Having both a
Customerclass and a
CustomerInfoclass is bad.)
- Don’t name it
accountListif it’s not actually a list data type.
- Member prefixes like
m_are not needed, your brain just ignores them (Android uses this convention.)
- Follow conventions so the code is consistent (it might break some of the other rules.)
- Don’t be cute (naming something like
- Don’t be clever (code obfuscation contests come to mind.)
- My opinion: a little on the longer side the better.
- Clarity is king.
- Class names should be a noun
- Method names should be a verb
- Don’t add gratuitous context (like
GSDMailingAddressfor an app called Gas Station Deluxe.)
- Avoid puns (
insertUserare pun names.)
- One word per concept (don’t have a
managerall in the same codebase.)
- Use searchable names.