in reply to Re: String equality and undef
in thread String equality and undef

On the other hand, dealing with a bogus value is usually shorter than what you do with a real value, and I think a good style guideline is to keep "else" as close as possible to the condition it negates.

if ( ! defined $id ) { die "Why is this ID not defined?" } else { # Twenty # or # more # lines }

I like this guideline because I've too many times found myself looking at a screen of code like this:

# blah # blah } else { return undef; } } else { unlink $tmpfile; return undef; }

What were the conditions?

Even better in this case would have been an early return so as to avoid the extra level of indent anyway, but that's beside the point.

Replies are listed 'Best First'.
Re^3: String equality and undef
by webfiend (Vicar) on Dec 09, 2008 at 16:36 UTC

    Fair point, but you don't really need to worry about the else when your reaction to the if condition is death.

    unless ( defined $id ) { die "Why is this ID not defined?"; } # Life goes on normally for everyone else.

    And I disagree with your closing, an early return in cases like that is exactly the point. You want to take any steps you can to make the code more readable, especially if it means completely ignoring my initial "switch the logic" comment.