[LRUG] To debug the impossible bug

Graham Ashton graham at effectif.com
Wed Aug 3 04:46:53 PDT 2011


On 3 Aug 2011, at 12:28, Jordi Noguera Leon wrote:

> I'd rather use:
> 
> def some_other_method
>   return foo.whatever if foo
>   return bar.whatever if bar
>   "some message"
> end

I don't want to pick on this specifically Jordi, but it raises an interesting issue.

I like to replace methods like that with a sequence of if/elsif/else. Putting the conditional statements at the beginning makes the conditional logic clearer, and doesn't require the reader's familiarity with a more esoteric idiom.

I did this very refactoring just yesterday and found a bug while I was doing it. The bug hadn't been visible when the code after "if" was off to the right of the screen.

Tom Christiansen wrote about an interesting approach to the relative merits of "foo if bar" versus "foo && bar" in the Camel book (Programming Perl). To paraphrase, he argued that you should put the most significant part of the statement first, where "significant" means it'll help your understanding of what the code is doing. As I interpret it, "if foo" is more important than "return foo.whatever" in the example above (in which control flow is of primary importance).

Hopefully this is making sense...

It might just be me, but it seems as though the Ruby community seems to choose idioms that optimise for fewer lines of code, rather than for clarity and ease of comprehension (the two are not the same).


More information about the Chat mailing list