2

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?

closed as primarily opinion-based by gnat, David Arno, user22815, Bart van Ingen Schenau, Blrfl Nov 28 '16 at 19:40

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • what language are you asking about, javascript? – gnat Nov 28 '16 at 16:03
  • "When I'm unsure of the argument's type?" This shouldn't be the case. If you are unsure about the type rethink the structure & name of your class, function[...] and whether it's actually necessary. – AMartinNo1 Nov 28 '16 at 16:04
  • I'm just wondering why you use arrow functions in this case... aside from the missing semi-colons. – Stephan Bijzitter Nov 28 '16 at 19:23
  • 2
    Value is a terrible name if you know what it's for or what kind of thing it's going to be. It's a fine name when you don't. That's why collections use it. – candied_orange Nov 28 '16 at 20:25
  • @Stephan Bijzitter Less visual noise. – alex Nov 29 '16 at 1:21
9

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.

  • So one should use item or value since an array could be composed of either strings or objects? – alex Nov 28 '16 at 16:39
  • 1
    I'm not aware of any such convention. value is simply an item in an array or collection, irrespective of its type. – Robert Harvey Nov 28 '16 at 17:12
2

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 array
  • keys: List the field names of an object
  • find: 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.

  • Thanks for the suggestion. Argument naming confuses me so much that I rather stick with a convention of four or five sometimes (e.g. value, key, array, string, object.) – alex Nov 29 '16 at 1:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.