Trying to understand when to prefer Kernel.length/1 vs Enum.count/1

Is Kernel.length/1 intended only for guards? I feel like I see people using length(items) in non-guard scenarios. Should I prefer it over Enum.count/1 in any general case?


Feel like I found my answer after I posted this.

Enum.count/1 delegates to length/1

2 Likes

A few more subtleties, Enum.count will destroy typing information; length will not.

Also by using length/1 the code is much more explicit - in my mind at least. Not having to wonder what the type of a variable is when reading a piece of code (given for example that Enum.count/1 supports all sorts of constructs) helps me read through a piece of code faster.

2 Likes