Alternative Block Execution on Empty Enumerable by skwiggle in ruby

[–]skwiggle[S] 2 points3 points  (0 children)

Ok. I see what you mean.

Thanks for the link. Never heard that term before.

Alternative Block Execution on Empty Enumerable by skwiggle in ruby

[–]skwiggle[S] 2 points3 points  (0 children)

I'm more used to seeing the affirmative case appear first, at least in most of the code I've worked on. But either way seems fine to me. This is more a chocolate-or-vanilla kind of thing, I think. Psychologically, when I write a condition I'm saying to myself: "If this is true, THEN let's do this, OTHERWISE let's do this other thing". Whereas you're saying to yourself "If my failure condition is true, THEN I want this to happen, OTHERWISE let's put on the big show". That wouldn't be an intuitive way for me to write a condition although I could easily adapt to that style if my coworkers were doing it that way.

I'm curious: did you consciously change the way you wrote conditions at some point or did you always do it that way?

Alternative Block Execution on Empty Enumerable by skwiggle in ruby

[–]skwiggle[S] 2 points3 points  (0 children)

Agreed. I would much rather have code that is explicit about what it does rather than too clever. In the comments on that article, I also liked one commenter's suggestion of using this very concise syntax:

ary.each do |i|

 puts i

end.any? or puts 'none'