The Roof Is Going to Go

Better Software Magazine
Volume-Issue: 
2007-04
Summary:

As one poor German tourist can attest, idioms don't translate. But Chuck Allison thinks programmers should become "native speakers" of the programming languages they use. This includes using and understanding them, idioms and all.

In October 1993, the Associated Press published an article about a German tourist on a US flight who approached the cockpit while the plane was taking off. When a flight attendant asked him to sit down, he said, "No, no, the roof would go!" Naturally, the flight attendant took that as a bomb threat, so the plane landed and the man landed-in jail. It turns out he just needed to use the toilet. According to the article, the phrase "Then the roof goes" is a German idiom for "having to go." It's too bad he spent nine months in jail.

Moral of the story: Idioms don't translate. It's difficult to learn to speak like a native in a new language, but it is doable and desirable. As programmers, we should use programming languages as they were intended, including understanding and using their idioms.

Speak Like a Native
Back in olden times, FORTRAN did not have an else statement or a compound statement construct. To implement an if-then-else construct, you had to use GOTOs (see figure 1).

That's just how it was done. It was a FORTRAN idiom. My second language was Algol. You'll never guess how I first rendered the fragment in figure 1 in that language (see figure 2).

Hey, I'm a math major-what did you expect? Not to worry; I soon saw the light. That is, I learned the Algol way (see figure
3).

Most programming languages have their individual claims to fame, which dictate when to use them and what common idioms apply. My first major project out of school was a flight planning system for the US Air Force. We used COBOL for the I/O and FORTRAN for the numeric processing, because that's where those respective languages excelled.

C and C++ are primarily systems programming languages; therefore, power and efficiency are top priority. The C mantra has always been, "Trust the programmer." It's true that not all programmers deserve that trust-programming outside of a sandbox requires some maturity. Many, for example, find idiomatic C code like that in figure 4 troublesome.

Using the expression *s++ is a well-entrenched idiom, but for many programmers the condition within the while statement should be a comparison rather than an assignment statement. This function is a simplified version of the standard strpy function, taken right from Kernighan and Ritchie's The C Programming Language. Here's what the authors say about it:

Although this may seem cryptic at first sight, the notational convenience is considerable, and the idiom should be mastered, because you will see it frequently in C programs.

One of the guiding principles of C++ is, "You don't pay for what you don't use." That's why there is no "mother of all classes" in C++ as there is in other object-oriented languages. Where you might use java.lang.Object in Java, you would more likely use templates in C++.

A common template idiom in C++ is known as CRTP, which stands for "Curiously Recurring Template Pattern." The code in figure 5 defines a base class that will make any of its derived classes a singleton. Deriving from Singleton requires CRTP.

The copy constructor and assignment operator are private and unimplemented, so no objects can inadvertently come to life by copy or assignment. The default constructor is protected, so only derived objects can invoke it. The type of the object to be made a singleton is the template parameter T. This means that T must be a template argument to Singleton at its own (i.e., T's) point of definition, as the code in figure 6 illustrates.

The first line may seem odd-defining a

File: 
AttachmentSize
The_Roof_Is_Going_to_Go.pdf197.2 KB

About the author

Chuck Allison's picture
Chuck Allison

Chuck Allison developed software for more than twenty years before becoming a professor of computer science at Utah Valley University. He is a technical editor for Better Software magazine and founding and current editor of the online journal, The C++ Source. He spent most of the 1990s as an active member of the C++ Standards Committee and is author of two C++ books, including Thinking In C++, Volume 2, with Bruce Eckel. His company, Fresh Sources, Inc., gives onsite training in C++, Python, and design patterns. His current top technical interest is the resurgence of functional programming. Whenever he finds a little down time he plays classical guitar or bikes the country roads of central Utah. Contact Chuck at chuck@freshsources.com.

Upcoming Events