Write BETTER Code! 7 Tips to Write Meaningful Names for variables, functions, classes

preview_player
Показать описание

Today we are going to speak about meaningful names so watch this video till the end and you will learn how to tell the difference between good code and bad code. You’ll know how to write good and meaningful names. And you’ll know how to transform bad names into good ones.

Names are everywhere in software. We name our variables, our functions, our arguments, classes, and packages. Because we do so much of it, we’d better do it well and in this case, don't be like Elon Musk.

1. Use Intention-revealing names
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.

2. Use pronounceable names
Humans are good at words. So make your names pronounceable. If you can’t pronounce it, you can’t discuss it without sounding like an idiot.

3. Use Searchable names
Single-letter names and numeric constants have a particular problem in that they are not easy to locate across a body of text. One might easily grep for MAX_CLASSES_PER_STUDENT, but the number 7 could be more
troublesome. My personal preference is that single-letter names can ONLY be used as local variables inside short methods. The length of a name should correspond to the size of its scope

4. Avoid Mental mapping
Readers shouldn’t have to mentally translate your names into other names they already know. This is a problem with single-letter variable names. Certainly, a loop counter may be named i or j or k if its scope is very small and no other names can conflict with it. This is because those single-letter names for loop counters are traditional. However, in most other contexts a single-letter name is a poor choice; it’s just a place holder that the reader must mentally map to the actual concept.

5. Class names
Classes and objects should have noun or noun phrase names like Customer, WikiPage, Account, and AddressParser. Avoid words like Manager, Processor, Data, or Info in the name of a class. Also, A class name should not be a verb.

6. Method names
Methods should have verb or verb phrase names like postPayment, deletePage, or save. Accessors, mutators, and predicates should be named for their value and prefixed with get, set, and is. For example getAge, setAge, isTeenager

7. Pick one-word per concept
Pick one word for one abstract concept and stick with it. For instance, it’s confusing to have fetch, retrieve, and get as equivalent methods of different classes. How do you remember which method name goes with which class? you spend an awful lot of time browsing through headers and
previous code samples. A consistent lexicon is a great boon to the programmers who must use your code.

The hardest thing about choosing good names is that it requires good descriptive skills and a shared cultural background. This is a teaching issue rather than a technical, business, or management issue.
Don't be afraid to rename things because Most of the time we don’t really memorize the names of classes and methods. We use the modern tools to deal with details like that so we can focus on whether the code reads like paragraphs and sentences. You will probably end up surprising someone when you rename, just like you might with any other code improvement.
Follow some of these rules and see whether you don’t improve the readability of your code. If you are maintaining someone else’s code, use refactoring tools to help resolve these problems. It will pay off in the short term and continue to pay in the long run.

#cleancode #programming #coding
Рекомендации по теме
Комментарии
Автор

What other tips and tricks you have for writing cleaner code?

Timecodes:
0:00 Intro: Clean code & Meaningful names
1:42 Intention revealing names
2:24 Pronounceable names
3:17 Searchable names
3:52 Avoid mental mapping
4:30 Class names
4:59 Method names
5:28 Pick one word per concept
6:07 Conclusion

notjustdev
Автор

I’m having my internship soon, this video will definitely help me not to look like a complete loser on my first day. Thanks!

andreiterus
Автор

Once I called my variable "variableB" using Russian "В" letter. Then I bought a new laptop and I din't have Russian alphabet on the keyboard. So, I had to take my new MacBook and replace its keyboard with the old one from my IBM laptop from 1999.

If I've watched this vedeo before, I wound't make such a

notJust.Daniil
Автор

#BlackLivesMatter #RussianLivesMatter #Nicevideo #Likes

notJust.Daniil