DEV Community

Discussion on: How JavaScript Variables Should Be Named

Collapse
reegodev profile image
Matteo Rigon

Not sure array_users is a good idea. What if you change the type to another iterable type? Do you rename all occurrences of the variable?

Thread Thread
markokolombo profile image
Marko Kolombo

Well, why would you change the iterable type in first place? (Not rethoric question)
And, in that case, would it be so complicated to make a replace all in the code editor?

Thread Thread
reegodev profile image
Matteo Rigon • Edited on

Maybe you'd like to have an object instead of an array to access users by their ID, or even better a Set. Heck maybe one day you wake up and think a linked list is the perfect solution.
Replacing the variable name per-se is not a problem, but it's still additional overhead.
Wouldn't it be better to name it like usersList and be done with its name forever?

Thread Thread
markokolombo profile image
Marko Kolombo

Absolutely right, but I'd like to stress that the core of my point is that the order should be hierarchy-based, from the most generic to the most specific.
Sure, first_name is more readable at first glance (btw only because of the english order of words), but that's just because you are not yet used to this system. From my experience, as you get used, it helps a lot to know immediately that name_first and name_last are names, and gives the naming a lot more consistency.
So, in the list case I'd use "list_users" : you immediately know that is a list (from the variable "users" we infer that it might be a list just arbitrarly), and that has type-relatives in "list_admins" and "list_employees"