When should I use “value” as an argument name? [closed]
https://softwareengineering.stackexchange.com/questions/337026
-
02-01-2021 - |
Question
I'm building my own utility functions to master ES6:
const contains = (array, value) => {
return array.indexOf(value) > -1
}
const keys = (object) => {
return Object.keys(object)
}
const find = (array, value) => {
return array.filter(item => {
return item[keys[0]] === value[keys[0]]
})[0]
}
So far, I've encountered only one problem: naming my arguments. I'm checking Lodash's naming conventions. I think it's clear when to use array
, object
, etc... I also saw the keyword value
. When to use this one? When I'm unsure of the argument's type?
Solution
In this context, value
is being used as a synonym for "item in an array or collection." It's as good a guideline as any.
OTHER TIPS
Try to describe in english what the code does. The words you'll find yourself using naturally when describing in english are great candidates for the names of the corresponding variables in the code.
(Of course, you may need some grammatical/syntactical transformations. In english you may say "an array" or "the array", but you don't want anArray
in your code - just array
is better)
This also works the other way around - try to describe in english using the names you've chosen, and see it it sounds correct:
contains
: Check if a value is inside an arraykeys
: List the field names of an objectfind
: Find the index of a value inside an array
The last one sounds a bit off - so maybe item
or entry
will be better? Try saying them out loud and choose the one that sounds best.