Skip to content

Instantly share code, notes, and snippets.

@mpj
Last active May 24, 2017 13:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpj/c9de0fb175b5aad4369f1067a046c040 to your computer and use it in GitHub Desktop.
Save mpj/c9de0fb175b5aad4369f1067a046c040 to your computer and use it in GitHub Desktop.
x is a pronoun

X is a pronoun

Syntax Sunday Snobbery!

Some people are annoyed when I use x as a parameter name in the functions passed to functions such as filter, map or find:

let kittens = animals.find(x => x.type === 'cat' && x.ageMonths < 12)

Instead, they would prefer to write the variable name out:

let kittens = 
  animals.find(animal => animal.type === 'cat' && animal.ageMonths < 12)

I personally find the first example parses just fine, and just think of it as a pronoun. I.e.

"Where is the knife? I cannot find it. Do you have it?"

vs.

"Where is the knife? I cannot find the knife. Do you have the knife?"

You should not overuse this of course, as it might create obscure code in that case, but when the name of the array is very physically close to the x, I think it's perfectly legible. Observe the proximity of "x" and "animals" here:

animals.find(x => x.type === 'cat' && x.ageMonths < 12)

What do you think?

@Tiagojdferreira
Copy link

I prefer animal in that example for the fact that if tomorrow a coworker changes that simple logic to a much more complex one, he will probably keep the same variable name.

@Tom-Bonnike
Copy link

Tom-Bonnike commented May 7, 2017

I personally prefer writing the full variable name. But then… What about:

animals.find(({ type, ageMonths }) => type === 'cat' && ageMonths < 12)

Where is the knife? I cannot find it. You know, the one with this specific handle and blade. Do you have it?

Is it more obscure? I don’t do that but well, since it’s Syntax Sunday Snobbery… 8-)

@jouni-kantola
Copy link

jouni-kantola commented May 7, 2017

  1. Let minifiers do what they're good at, shortening words we type for others to read.
  2. For the specific case you're describing here, I'd say it would be clearer to write:
let kittens = animals.filter(animal => animal.type === 'cat').filter(cat => cat.ageMonths < 12)
// is it one or many kitten you're looking for btw?
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

@jouni-kantola
Copy link

@Tom-Bonnike: I think you nailed it. If there's a possibility to focus on what to be done over how it's done, then that's the way to go about it. Your suggestion is clear that we're looking for an animal that is a cat and less than 12 months old. From that aspect I do like filtering out this way instead:

const kittens = animals.filter(onlyKittens)

// then the less important 'how'
const onlyKittens = ({ type, ageMonths }) => type === 'cat' && ageMonths < 12

@coot
Copy link

coot commented May 7, 2017

I prefer to use PureScript and stay on the type-safe side ;)

@pakoito
Copy link

pakoito commented May 7, 2017

In Scala and Kotlin you can skip the single parameter of a lamba using an implicit name. For example, in Kotlin:

animals.find { it.type === 'cat' && it.ageMonths < 12 }

@byxor
Copy link

byxor commented May 7, 2017

How about the unprecedented midwife approach:

let myArrayOfKittens = myArrayOfAnimals.find(
  potentialAnimalUnlessSomeonePutSomethingElseInTheArray =>
  potentialAnimalUnlessSomeonePutSomethingElseInTheArray.type === 'cat' && 
  potentialAnimalUnlessSomeonePutSomethingElseInTheArray.ageMonths < 12
)

(Just kidding)

Honestly I don't mind either. x is short and sweet, animal is unambiguous and not much longer.

@dmail
Copy link

dmail commented May 7, 2017

From my point of view, using x avoid the effort to name the variable.
In the end that makes you faster but also makes your code a bit harder to understand.

In your example, the difference is negligible but I would take the most powerful strategy and apply it everywhere.
Which is, for me, naming the variable animal rather than x. It gives more meaning.

@DannyFeliz
Copy link

I still prefer to use animal instead of x in the example

@babbage
Copy link

babbage commented May 7, 2017

Definite preference for animal over x. Far more readable. Remains readable even if context changes around it in the code. Additionally, to me, x implies the type of the object is unknown and cannot be expected. animal implies that you have a specific expectation about what it will be.

@nifo
Copy link

nifo commented May 11, 2017

I would prefer to use a instead of x.
let kittens = animals.find(a => a.type === 'cat' && a.ageMonths < 12)
Since x is usually for unknowns and here it's not. If more variables also starting on a is needed, it would make sense to consider changing to ani or animal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment