[UKLUG] Re: LPM: unless ($line =~ /^#/)

Wesley wsheldahl at qx.net
Tue Jan 2 08:09:52 CST 2001


> > Well, yes, you can do unless, else, but it seems rather backwards. It's
> > good style to program as you would speak or think. unless ... else  is
> > potentially confusing, and I would think that it would be a good idea to
> > avoid it.

I would agree here.  If you're going to have an else clause, you could
probably just as well put the else clause first under an "if."
 
> Unless may be great for throw-away programs, where you're trying to code
> quick, but as you can always negate the test in your condition, it's
> normally easier to maintain when you always use 'if'.
> 
> Besides, it's fewer keystrokes to type:
> 
>         if ($line !~ /^#/)
> 
> vs.
>         unless ($line =~ /^#/)
> 
> -----
> Joe Hourcle

If the boolean expression is that simple, you're probably right.  If the
if clause is more complex, then I think the unless is often more
readable:
 
	if (not($hairy_compound_boolean_expression)) {
 
vs.

	unless ($hairy_compound_boolean_expression) {

That removes a set of parentheses and should still be readable. 
Assuming there's no else at the end of it though, because the else would
give a better opportunity to clarify.

I can think of a more confusing structure:

	unless (...) {
		...
	}
	elsif (...) {
		...
	elsif (...) {
		...
	}
	else {
		...
	}

:-)
	
-- 
Wes Sheldahl
wsheldahl at qx.net



More information about the Lexington-pm mailing list