Why I don't like inverted if conditionals

June 28, 2007

Perl has an often used feature where you can write a conditional statement in a form like:

statement if (condition);

(You can use unless too.)

I find this usage lurching because it's a dislocation in my flow of thought. I start out reading the code thinking that the statement always happens; then I get to the if and whoops, time to backtrack on my understanding.

The worst form of this is when the if is indented a lot, so it's almost flush right with a lot of whitespace between it and the main statement. My eyes and mind have been carefully trained to expect that things hanging out by their lonesome in the right margin will be comments, not code that affects the program flow.

(At one point I actually wrote Perl code that looked like this. I tell myself that I know better now, but I'm probably doing equally heinous things somewhere else. Reading your old code can be a very humbling experience.)

Perl is not the only language that does this (there's Ruby, for example, and Python 2.5's conditional expressions also work this way). While Perl is the only one of them I currently have direct experience with, I don't think I'm going to like this any better in another language, Python included.

Written on 28 June 2007.
« Why you can't use object.__new__ on everything
Why forwarding all email for users is dangerous »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Thu Jun 28 23:29:46 2007
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.