http://www.perlmonks.org?node_id=727827


in reply to Code Maintainability

Thanks for your meditation. You've given me some food for thought.

I like statement modifiers for short statements.

upchuck() if ! $healthy;

For a much longer statement, I find the modifier gets lost, and it gets harder to understand what's going on.

Exception::Of::Doom->throw( error => 'Detailed error messages are impo +rtant, especially when this error happens' ) if $oops;

I think there are plenty of times—perhaps even most times—that I'd rather read "if not" rather than "unless". I've seen programmers use "unless" almost universally for error conditions, and that seems like a pretty good convention, but even then I think it depends somewhat on what's in the condition.

upchuck() unless $healthy; # doesn't sound right error() unless happiness_ensues(); # better?

The other thing about statement modifiers is they make it harder to add stuff to the condition.

if ( ! $healthy ) { upchuck(); die; }

If it were written as three lines to begin with, I wouldn't have to go through so much trouble to add the one extra action.

Sometimes "maintainability" is about "editability" rather than "readability". Yes, statement modifiers allow you to get more on the screen, and it may flow more naturally before your eyes. Now insert some debugging statements. When it barfs on line 123, is that the stuff in the condition, or in the condition's result?

I don't always write "one thing per line", but I see its advantages.

As an aside, this looks clumsy to me:

print STDERR join("\n", @lines), "\n";

I've sometimes written that as:

print STDERR map { "$_\n" } @lines;