Here’s some patterns I’ve noticed in what I consider high quality code when it comes to coming up with names for identifiers for, e.g., variables.
Names should be as verbose as necessary to understand the meaning in the context in which the identifer is found, and not more than that. Too verbose and the code is bulky, hard to edit, and hard to understand by glancing over. Not verbose enough and the code is unclear in meaning. Context here could mean the project it is in, the function the identifier is used in (e.g. as a parameter), or the class it’s a member of.
-
Describe the identifier using only words necessary to clarify its meaning in the given context. If you have a function declaration
move_atom(AtomID id)
that simply changes the position of the given atom, it’s sufficient for the parameter to be just namedid
, instead of, e.g.atom_id
. However, theatom_
prefix might be useful in a function that deals with multiple types of IDs. So if a function also deals with abond_id
, given the situation, it’s good to add the prefix to clarify the distinction between the two IDs. -
Ordinal identifiers should only be used if the order matters. If you have a function declaration like
angle(Point pt1, Point pt2, Point pt3)
, the order would matter ifpt2
is the vertex of the angle. However, if you haveplane(Point pt1, Point pt2, Point pt3)
, the order of the points doesn’t matter and a naming scheme that doesn’t necessarily imply the order is better. So something likeplane(Point pti, Point ptj, Point ptk)
would be a better option.The rationale is that the code that calls
plane
might have a bunch of points, such aspt1
,pt2
,pt3
,pt4
, …ptn
. And this code might need to call plane likeplane(pt2, pt3, pt4)
. If we declareplane(Point pt1, Point pt2, Point pt3)
, a static code analyzer, or a person reading the code, might flag this as a bug, seeing as we usedpt2
in place of parameterpt1
, andpt3
in place of parameterpt2
. To avoid confusion with ordering, if the order doesn’t matter, the identifiers should signal that. -
Abbreviations should be used in two cases:
- When they’re commonly understood. E.g.
num
is commonly understood to meannumber
andidx
to meanindex
. - When the meaning is clear from the context. E.g. if you have a class
Projectile
,pos
,vel
, andaccel
are sensible names forposition
,velocity
andacceleration
members as the context clarifies the abbreviations. In a different scenario, these names, and especiallyvel
might not be clear.
- When they’re commonly understood. E.g.