clojure-style-guide icon indicating copy to clipboard operation
clojure-style-guide copied to clipboard

Use empty? to determine whether a seq is empty

Open AlexBaranosky opened this issue 11 years ago • 4 comments

empty? is simple, direct, and effective. The use of seq as a drop-in replacement for (complement empty?) is not style worth propagating, because it doesn't encourage straight-forward code readability.

AlexBaranosky avatar Jan 05 '13 22:01 AlexBaranosky

I'm pretty sure "Joy of Clojure" had a section on the topic and the advice was the same. At least in loops it does make sense to me.

bbatsov avatar Jan 05 '13 23:01 bbatsov

I'm not sure if that comment is agreeing or disagreeing with me, but assuming it is disagreeing, I would ask what empty? is for if not this use?

AlexBaranosky avatar Jan 05 '13 23:01 AlexBaranosky

@AlexBaranosky I hear you. This is a matter of preference, really. I personally use empty? or seq depending on what makes my boolean logic simpler. For example, I would always prefer (if (seq x) ...) to (if (not (empty? x)) ...). Do you prefer using empty? unconditionally?

ghoseb avatar Jan 06 '13 04:01 ghoseb

I prefer using empty? but Rich encourages using seq as a predicate. There isn't widespread agreement on the topic.

technomancy avatar Jan 06 '13 05:01 technomancy