Higher order variable names

As far as readability goes, is using “x” and “y” okay in higher order callbacks since everything is scoped? Just seems to shorten everything and is just as easy to understand. Just not sure what colleagues would think of it;
currentMonth = (x) => (x === “January”);
currentMonth = (month) => (month === “January”)

In my opinion, is perfectly fine as long as the code in general is readable by humans and the code works as its suppose to; I’ll just suggest that you use variables names that are specific to their value, i.e. using variable “x” to refer to a user’ s bank account will make less sense than using variable “userAccount”

I try to keep the variable names as descriptive as possible, if things seem to not fit, I break into new lines without taking away from readability. I also have the habit of naming unused variables as _ because that’s the functional pattern-matching way.

I’m not opposed to using xs and ys but I only do that when I’m doing quick testing and I don’t have time to type actual semantic variable names.

1 Like

I would go with as descriptive as possible.

Even in your example above, someone who’s never read the code before would have to take a few more seconds to figure out what x is versus month.

Likely hood is you’ll be minifying the code for production, which will generally shorten variables to single characters anyway, so using longer variable names in source code won’t hurt performance later on.

I keep my variable names as descriptive as possible. The meaning behind shorter names is easier to determine when references are clustered together and are adjacent to the variables definition. However, as the code base grows and references disperse it becomes harder.

Another argument for descriptive names is that at the time you write a piece of code the meaning of a short variable name is fresh on your mind. But, as time passes we become less familiar with the code and just as importantly, it’s context. This makes discerning the meaning behind a non-descriptive name harder as well.

Although a shorter variable name may save some typing these savings are minuscule when factored over the lifetime of the code base. The only exception I see is for index variables used in loops. Since the context is derived from the object name that’s being referenced and assuming that the looping logic clearly shows that the index is being used to pass over a collection of objects then there’s little downside to using a shorter name. For example, a for loop using i as the index variable.

Just my opinion though. I’m interested in hearing what others think about this as well.