I am reading articles on medium for about 6 months. I am writing my first story for developers Community. Going to write some rules for naming from book Clean Code.
Names are everywhere while writing code. We name variables, objects, functions, classes, libraries, packages. We name so much so we should do well. Writing some simple rules that might help in dealing with information within those names.
Use Intention revealing Names
Name should tell you why it exists, what it does, and how it is used.
int d; // time in days [If a name requires a comment, then the name does not reveal its intent.]
Name d reveals nothing. It doesn’t describe sense of time or days.
We should choose a name that specifies what is being measured and the unit of that measurement:
int elapsedTimeInDays;
Avoid Disinformation
Programmers must avoid leaving false clues that uncertain the meaning of code. We should avoid words whose entrenched meanings vary from our intended meaning. For example, hp, aix, and sco would be poor variable names because they are the names of Unix platforms or variants. Even if you are coding a hypotenuse and hp looks like a good abbreviation, it could be disinformative. Beware of using names which vary in small ways.
Make Meaningful Distinctions
Below is real time example which took extra time to spot and took conceptual power to understand difference between $this->data and $data [two different array]. They didn’t evoke any information about data. Also $merged_array is bad name.
Always put time to name variable. It looks it takes time but I doesn’t.
Use Pronounceable Names
- genymdhms (generation date, year, month, day, hour, minute, and second) [Bad Name]
- generationTimestamp [Good Name]
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.
Class Names
- Classes and objects should have noun or noun phrase names like Customer, WikiPage, Account, and AddressParser
- A class name should not be a verb.
Method Names
- Methods should have verb or verb phrase names like postPayment, deletePage, or save
Use technical names
- Use solution domain names
- Use problem domain names
Keep Learning !!