Channels ▼

Andrew Koenig

Dr. Dobb's Bloggers

Dialects and Readability

November 16, 2011

Last week, I noted that if there were clear rules about what programming constructs programmers should and shouldn't use, those rules effectively created a dialect, different from the language's nominal definition, in which programmers were actually writing their code.

Last month, I gave an example of a usage that, although I think it has clear advantages over how programmers normally write such programs, has the disadvantage of being unfamiliar:

for (string line; getline(cin, line); ) { 
   // Process a line of the file 

This example reads each line of the standard input successively into the variable line, and then processes the line. Its advantage over the usual style is that it makes line into a local variable; there is no risk of using line by mistake after the loop has finished. In spite of clear advantages, programmers do not seem to be eager to adopt this technique.

Here are two other examples of programming styles that have not been widely adopted despite what I think are significant advantages.

The first is to put literals on the left side of relations, particularly assignment. In other words, instead of writing

if (n == 42) { /* … */ }

the style advocates writing

if (42 == n) { /* … */ }

The advantage of this style should be easy enough to see: A programmer who writes = instead of == will be greeted by a compiler error message rather than having a program that quietly sets n to 42 and then compares it (implicitly) with zero. People may argue that 0<n is harder to understand than n>0, but the counterargument is at least plausible that the difficulty comes solely from lack of experience.

The second example is putting modifiers such as const after the types they modify rather than before them:

int const n = 42;  // rather than const int n = 42

The reason for this style is that when pointers are involved, writing

int const *p;

makes it clearer that p is a pointer to an int const, rather than that p is a const that points to an int. Writing

const int *p;

makes it unclear whether const is intended to apply to int *p or just to int; the alternative form makes it clear that const modifies only int.

Questions for readers: Do you think that the advantages of these dialects outweigh their unfamiliarity? Have you encountered other similar usages?

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.