Syntactical Sugar: conditionals
jd / Mon, 05 Mar 2007 19:19:00 GMT
This article has moved over to our sister spin-off blog, RubyJudo, which focuses on more arcane technical topics than NotRocketSurgery.
jd / Mon, 05 Mar 2007 19:19:00 GMT
This article has moved over to our sister spin-off blog, RubyJudo, which focuses on more arcane technical topics than NotRocketSurgery.
Yes I would say I use that last syntax quite a bit. If the expressions are simple, it ends up looking better. If you have multiple branches (else if) or if the expressions are complicated in any way, it could get ugly.
Speaking of which, does ruby code end up in smaller, more manageable chunks, from smaller methods to less complex expressions, all on its own, or is it just me?
Yes, if the condition is complicated, then a 3+ line block make sense. For multi-line conditionals, I bring back use of the
thenkeyword for readability as well:To some degree, Ruby helps keep your code simple. I think this is because you have more direct tools to accomplish what you need to do, like blocks and methods like
#collectand#inject. However, keeping your code comprehendible often takes conscious effort—in other words, I’ve still seen some pretty badly written Rails code.