How do you consistently name things?
Cliff Brake October 10, 2024 #consistency #namingThere are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton
Plenty of opinions abound on style, naming conventions, formatting, etc.
But there is one principle I try to follow when naming things -- go from general to specific. Example:
UserEmailAddress
For functions, you may have a module that has multiple operations. In this case, the operation is a specific operation of a more general module.
atmtcp_v_dev_open()
atmtcp_v_dev_init()
atmtcp_v_dev_close()
Even though open_atmtcp_v_dev()
reads better as an English sentence. Programs can be thought as both prose and data, and sometimes there are advantages to considering function and variable names as data.
General->specific naturally organizes things. It is easy to browse variables in an editor that may list all the variables in a module. Searching/refactoring is also easier.
Sometimes when dealing with two unrelated types of information, it is not obvious which is more specific. In the below example, VINST/VPEAK
are measurements (type), and AX/AY/...
are channels (instance). Which should go first?
VINST_AX
VINST_AY
VINST_AZ
VPEAK_AX
- ...
Generally I prefer to put the "type" first, and then the "instance" -- especially if there are multiple instances. But this could probably go either way.
Another way to think about this is VINST
may be a map/dictionary/hash-table property of an object, and AX/AY/AZ
may be keys into this map.
Use nouns for variables and verbs for functions.
Does this make sense? How do you name things?