First of all, although one check is nil? maybe faster than block_given? block grip is slow. Therefore, if you are not going to capture it anyway, the performance argument is not valid.
Secondly, it is easier to understand. Whenever you see block_given? , you know exactly what is going on. When do you have x.nil? , you need to stop and think about what x .
Thirdly, this is an idiom. In my experience, the vast majority of Ruby developers will prefer block_given? . When in Rome ...
Finally, you can keep it consistent. Should you always use block_given? , the problem is solved for you. Should you use nil? checks nil? You must block the block.
- Performance overhead.
- This is more detailed, something rubists try to avoid.
- Naming things is one of the most difficult in programming. :) Can you think of a good name for the
Enumerable#map block, for example, get it? - "Grepability" is a desirable feature for the codebase. If you want to find all the places where you are checking if a block has been provided to you,
nil? execution nil? inspections can be difficult.
ndn
source share