The shortcut if

In a paper critiquing Pascal in 1973, A. N., Habermann comments on the if statement. He suggested that the code:

i := if i=7 then 1 else i+1

more clearly expresses that a value is assigned to i than the statement:

if i=7 then i:=1 else i:=i+1

What do you think? Is the embedded statement easier to read? Maybe, for an experienced programmer, maybe not so much for a novice. It is similar to the problem found in C with the ternary if statement.

i = i==7 ? 1 : i+1;

This suffers from a lack of readability, mostly related to the use of two symbols ? and : to represent then and else. Using the actual words if, then and else would be two verbose. That and it doesn’t seem logical to everyone to embed a decision statement within an assignment. But it does make nice compact code.

Habermann, A.N., “Critical comments on the programming language Pascal”, Acta Informatica, 3, pp.47-57 (1973).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s