rrrene / credo-proposals

Proposals for Credo, the Elixir code analysis tool with a focus on code consistency and teaching
MIT License
9 stars 0 forks source link

Proposal: Anonymous function style #11

Open PragTob opened 7 years ago

PragTob commented 7 years ago

Environment

(shouldn't matter couldn't find the check on master/in my project with 0.8.4 if it does exist please tell me)

The Check

This might be me personally, but I strongly dislike the anonymous function & short cut, mostly because it circumvents the problem of naming your parameters and is a slightly arcane syntax for something that has a (imo) clearer syntax:

iex(1)> Enum.map([1, 2, 3], fn(i) -> i + 1 end) 
[2, 3, 4]
iex(2)> Enum.map([1, 2, 3], &(&1 + 1))         
[2, 3, 4]

(I know the sample doesn't exactly support my naming claim but in most real applications we map over something meaningful to our domain)

I'd like to have an optional check to replace usages of the & short cut with the more verbose fn. I know people might feel differently which is why I'd like to have it optional.

Any place for such a check in credo? :)

rrrene commented 7 years ago

@PragTob I see your point, but I am not overly optimistic that many people share your disapproval at this point.

That being said, if anybody feels strong enough about this to make a descent PR, I will probably merge it.

We have a couple of these "controversial" checks in Credo and I can see how this one might be useful to people sharing your opinion. :+1:

asummers commented 7 years ago

One comment I would make here is that while I generally agree with the dislike of the

Enum.map([1, 2, 3], &(&1 + 1)) 

syntax, something like

Enum.map(my_maps, &Map.get(&1, :id))

does not suffer from the same problems, to me. Just something to consider as a potential toggle if someone decides to implement.