Style Case Studies: Index Tables - Correcting Mechanical Errors
(Page 3 of 6 )
a) Mechanical errors, such as invalid syntax or nonportable conventions.
The first area for constructive criticism is mechanical errors in the code, which on most platforms won’t compile as shown.
#include <algorith>
1. Spell standard headers correctly. Here the header <algorithm> is misspelled as <algorith>. My first guess was that this is probably an artifact of an 8-character file system used to test the original code, but even my old version of VC++ on an old version of Windows (based on the 8.3 filename system) rejected this code. Anyway, it’s not standard, and even on hobbled file systems the compiler itself is required to support any standard long header names, even if it silently maps it onto a shorter filename (or onto no file at all).
Next, consider:
main()
2. Define main correctly. This unadorned signature for main has never been standard C++ [C++98], although it is a conforming extension as long as the compiler warns about it. It used to be valid in pre-1999 C, which had an implicit int rule, but it’s nonstandard in both C++ (which never had implicit int) and C99 [C99] (which as far as I can tell didn’t merely deprecate implicit int, but actually removed it outright). In the C++ standard, see:
- §3.6.1/2: portable code must define main as either int main() or int main( int, char*[] )
- §7/7 footnote 78, and §7.1.5/2 footnote 80: implicit int banned
- Annex C (Compatibility), comment on 7.1.5/4: explicitly notes that bare main() is invalid C++, and must be written int main()
Guideline: Don’t rely on implicit int; it’s not standard-conforming portable C++. In particular “void main()” or just “main()” has never been standard C++, although many compilers still support them as conforming extensions.
cout << “#################” << endl;
3. Always #include the headers for the types whose definitions you need. The program uses cout and endl but fails to #include <iostream>. Why did this probably work on the original developer’s system? Because C++ standard headers can #include each other, but unlike C, C++ does not specify which standard headers #include which other standard headers.
In this case, the program does #include <vector> and <algorithm>, and on the original system it probably just so happened that one of those headers also happened to indirectly #include <iostream> too. That might work on the library implementation used to develop the original code, and it happens to work on mine too, but it’s not portable and not good style.
4. Follow the guidelines in Item 36 in More Exceptional C++ [Sutter02] about using namespaces. Speaking of cout and endl, the program must also qualify them with std:: or write using std::cout; using std::endl;. Unfortunately it’s still common for authors to forget namespace scope qualifiers—I hasten to point out that this author did correctly scope vector and sort, which is good.
This chapter is from Exceptional C++ Style, by Herb Sutter (ISBN 0201760428, copyright 2005. All rights reserved. It is reprinted with permission from Addison-Wesley Professional). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Improving Style >>
More Code Examples Articles
More By Addison-Wesley/Prentice Hall PTR