On Github jcabas / meaningful-names
Jonathan Andrés - @jcabasc
The way the I see, coding is like “writing a book, or an article in the newspaper”, you need to express yourself so good in a way the people would buy it.
That means, you have to be as clear as possible and release something understandable, no matter what language you’re using. You have to deliver clean code.
The name of a variable, function, or class, should answer all the big questions. It should tell you why it exists, what it does, and how it is used. If a name requires a comment, then the name does not reveal its intent.
// what are these? int ca; int ua; def get_dau self.order(:created_at).all.map do |item| [item.created_at, item.active_users.to_i] end def get_mau self.order(:created_at).all.map do |item| [item.created_at, item.unique_active_users] end end
There is no need to call a variable customerArray if it's an active relation object.
A good name contains as many words as are needed to express a concept. But nothing more. Any unnecessary words make the name longer and harder to understand. Short names are good only when they describe the whole concept in the current context (it is better to say “customersInOrder” than “list” in a context of making an order).
// Long variables ABCModuleForEfficientHandlingOfDates ABCModuleForEfficientStoringOfDates customerList // hash quantity //percentage int a = l; if ( O == l ) a = O1; else l = 01;
it's unlikely that this happens, but in this situation it's indistinguishable zero (0) from uppercase (O) and one (1) from lower-case (l).
For instance if you call a variable member2 because you already have another one call it member1.
For instance if you have a class named Product, and create two more classes ProductInfo and ProductData. You have made the names different without making them mean anything different.
// get_active_ccount() get_active_account_info() def copy_content(a1, a2) { for (int i = 0; i < a1.length; i++) { a2[i] = a1[i] } } Class CustomerInfo end Class CustomerData end
How we supposed to know which of these functions to call?
In the secod example, this function reads much better when source and destination are used for the argument names.
People are good at words, so take advantage of that. Make your names pronounceable.
Need no explanation.
Names should be easy to locate across a body of text.
def message MESSAGE if total > 0 && percent_with_prices <= 10 end
Single-letter names and numeric constants have a particular problem in that they are not easy to locate across a body of text.
Single-letter names can ONLY be used as local variables inside short methods.
Encoding type or scope information into names simply adds to the burden of deciphering. They are a mental burden, seldom pronounceable and easy to mistype.
Need no explanation.
This problem generally arises from a choice to use NEITHER problem domain terms nor solution-domain terms.
There is no need to call a variable for its synonyms or something you think is similar within a context.
For instance if you're working in a healthcare software, and there are patients and doctors, but you decide to call them users within a scope.
Self-explnatory.
Methods should have verb or verb-phrase names, like deletePage or save.
This apply for public methods. There are exceptions when you could call private methods by a word that represent the name of the object that return (Personal opinion).
Don’t be too clever or humourous with your names.
Name things as what they are.
Self-explnatory. In spanish "No inventes". Don't you remember the vander_fix method.
For instance if you're retrieving attributes from an API in a User class, you should not call it any different in another one when you're doing the same thing.
Avoid using the same word for two purposes. Using the same term for two different ideas is essentially a pun.
For example using the term add in several classes, for “consistency”, when you are in fact not using it in the same sense. Adding an element to an array or concatenating two values.
The people who read you code are programmers, so go ahead and use computer science terms, algorithm names, pattern names, math terms, and so forth, such as AccountObserver, where Observer means the Observer pattern.
If the reader is familiar with the concept, might clarify to him what is its purpose.
To make code more understandable to anyone involved in solving a problem, it is better to use meaningful names in a domain context.
That means, use business language.
All code that programmers write is connected to some domain logic.
In the future somebody else (not only a programmer, maybe a tester) will use your code and will be able to easily understand it in a domain context (with business logic knowledge).
The context is very important to understand a name, because it has additional information.
You can add context to names by using prefixes or enclosing them in well-named classes, functions, or namespaces.
Imagine that you have variables named city, state, and zipcode. Taken together it’s pretty clear that they form an address. But what if you just saw the state variable being used alone in a method? Would you automatically infer that it was part of an address?
You can add context by using prefixes: addrCity, addrState, addrZipcode or by creating a class named Address.
A good name contains as many words as are needed to express a concept. But nothing more. Any unnecessary words make the name longer and harder to understand.