Programming syntax and its analogy with English (i)

One of the problems with the structure of programming languages lies in its choice of English words for syntax, partially because they can be misleading.. From an early stage in the evolution of programming syntax the choice of keywords in a language has been focused on words that suggest something about their effect: PRINT, READ, WRITE, REPEAT. For instance the use of PRINT likely evolved because the early programs output to teletype machines as opposed to the screen. Many of these words have evolved into common use, however to the novice programmer, some may have more than one meaning.

Let’s consider the keyword if, commonly found is nearly every language to actuate a decision statement. In the dictionary, it is either a conjunction or a noun. As a conjunction it introduces a conditional clause.

if it is raining, open an umbrella.

Yet it can also be used to express surprise, as in “Well, if it isn’t Obi-Wan Kenobi”. Is there a better word for a conditional? What about whenever? assuming? or even while? Let’s consider an example piece of code.

whenever (x > 0)
    r = sqrt(x)

This doesn’t work so well, because whenever implies “every time that”, which seems more like its associated with some repetitive structure (similar to while). The keyword assuming might work equally as well as if, but only if used in combination with the keyword then. So the key to understanding syntax might be the interplay between different keywords in a control structure. The classic is of course if-then-else, which if often expressed as if-else in languages such as C. Part of the reason these combinations have been successful may be their brevity. Imagine if alternate words had been used:

assuming (x > 0) it follows that
    r = sqrt(x)
otherwise 
    r = 0

Just as readable, but more verbose. Could have worked well in Cobol, but contemporary programmers don’t like verbosity (this is the feedback I have received from numerous students when confronted with programming in Cobol). In any case, the use of a complete if-then-else sequence would likely make it easier for novice programmers to understand the context and scope of a decision.

if (some_condition) then
    perform an action
else 
    perform an action
end

 

 

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