Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Used any? instead of length call #3779
Thanks but in general we don't do only style changes commits in Rails. This is to avoid a sequence of unnecessary pull requests that ends up replacing all entries of length > 0 by any? in Rails. That said, I am merging this one but following ones will be closed. Thanks!
added a commit
this pull request
Nov 28, 2011
Thanks for guidance will take care of it. Intension was to avoid two different call one for length and then for checking for > 0.
I am going through the commits and rails code. I have replace in one place but found that it is used in other places also so I have modified all files where it was.
Anyways really thanks for guidance.
@rahul100885 this benchmark does not seem to back up the micro-optimization
In addition to that, we are using a method call that it is not equivalent. I don't want to be thinking that if errors has items then they are true and so any? is checking for emptyness. length > 0 involves no extra brain cycles.
In summary I believe this is neither an optimization nor more readable.
@rahul100885 oh by the way, I see your readability point in having a predicate instead of length > 0. But the problem here is that any? reads like English, but it does not mean the same as in English, there's the nuance of being true in Ruby. The empty? predicate on the other hand allows you to have a really equivalent alternative.