Channels ▼

Andrew Koenig

Dr. Dobb's Bloggers

Declarations and Reliability

September 21, 2011

When I wrote about auto a while ago, some readers said that they thought auto was a bad idea because it allowed programmers to get by without knowing what types they were using. I explained my thinking about this issue. Nevertheless, the discussion left me with a nagging doubt, which I would like to discuss here.

The first programming language I learned was FORTRAN, which had (and still has) the interesting characteristic that if you do not declare a variable, the compiler creates it for you. In fact, the early version of FORTRAN that I learned did not even allow users to declare variables. So, for example, if you intended to write

FOO = 1.0

and actually wrote

FOE = 1.0

and later wrote

FOO = FOO * 2.0

the compiler would not complain that you had a variable named FOE that you never used, nor would it complain that you used a variable named FOO without giving it an initial value, and would blithely take whatever garbage was in the memory assigned to FOO and multiply it by 2.0, thereby yielding other garbage.

I remember reading the claim — though I don't remember where — that one of the great advances of Algol over other languages (presumably FORTRAN) was that it required its programmers to declare every variable. In effect, when you write an Algol program, you have to state the name of every variable at least twice: once when you declare it, and then again when you use it. If those two statements don't match, the compiler will reject your program.

The question, then, is this: If it is a good thing that a compiler requires you to state the name of every variable at least twice, why is it not a bad thing when the compiler figures out the types of your variables for you? For example: auto it = v.begin();

Why should the programmer not be required to know that v is a vector<string>, and therefore be required to write

vector<string>::iterator it = v.begin();

or something similar?

After thinking about it carefully, I believe that the difference between these two cases is that FORTRAN not only creates a variable if you do not declare it, but it also creates a value for that variable. To see why FORTRAN's behavior is dangerous, consider what would happen if a Python programmer were to try to execute

FOE = 1.0
 FOO = FOO * 2.0

When it came time to execute the second statement, the implementation would complain that no variable named FOO exists, because in Python, a variable does not exist until a value has been assigned to it.

Similarly, in C++, when I execute

auto it = v.begin();

I am not asking the compiler to invent anything for me. I've written v.begin(), which is a legitimate expression, and I've said that I want to put the result of that expression somewhere. If I want to use that result, then its type is going to have to be consistent with the way(s) in which I intend to use it, so the compiler will check whatever aspects of the result's type are necessary in order for me to use that result.

In short, although I agree with the claim that Algol's requirement to declare variables makes programs more reliable, I now think that the reason for this reliability is not that it requires programmers to state the name of every variable twice. Rather, I think the problem with the FORTRAN treatment is that the compiler tries to guess what the programmer meant. In cases such as auto, no guesswork is involved.

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.
 


Video