3 ms·
I find that display_warning() if problem_condition() feels much more expressive in some situations than if(problem_condition) display_warning() The
by joe_the_user 16y ago
I find that
display_warning() if problem_condition()
feels much more expressive in some situations than
if(problem_condition) display_warning()
The later just emphasizes the primary activity to me - again, in some circumstances. It's something I miss now that I'm not doing Ruby anymore.
- mahmud 16y agoThat breaks efficient code reading. When you read code, you tend to skip through basic-blocks (i.e. consecutive statements without branches) and follow them to that intersection point where a decision has to be made. In traditional form, if you have: if (condition) consequence(); You're able to "run" the condition in your head and step into the consequence without loss of pace. But when you put the consequence before the condition, you will have to backtrack.
- v_ 16y agounless the consequence itself is something that signifies the end of a basic block, like a break or a next or a return.
- brianto2010 16y agobacktrack = true unless consequence.signifies? :block_end It doesn't seem that much harder to read.
- draegtun 16y agoUnfortunately though you are potentially adding subtle bugs here which can only be caught at run time. To avoid excessive test coverage it would be more pragmatic to do: backtrack = (consequence.signifies? :block_end) ? false : true But that's not as easy to read :(
- mahmud 16y agoI didn't reply to that one-liner because it is a one-liner. A lot of software engineering lore crumbles into tautologies or is completely obviated when you're working at the microscopic level.