diff --git a/coding-standards.html b/coding-standards.html index f2b603c02..a796f966c 100644 --- a/coding-standards.html +++ b/coding-standards.html @@ -64,9 +64,10 @@ whatever standards are chosen are used consistently throughout a project. This document describes the coding style already in use throughout the Celestia code base. As Celestia grows, there will undoubtedly be clarifications, additions, and edits made to this document, and I encourage -suggestions. However, no recommendations will be entertained which require -reformatting or renaming huge chunks of the existing code. No stylistic -convention is so inherently superior that it's worth all that hassle. +suggestions. However, recommendations that require +reformatting or renaming huge chunks of the existing code will be ignored. +No stylistic convention is so inherently superior that it's worth all that +hassle.
// Celestia style char* str; +str = reinterpret_cast<char*>(p); // Not Celestia style char *str; +str = reinterpret_cast<char *>(p);
The Scope of for
+The ANSI C++ standard states that the scope of a variable declared in
+the initialization part of a for statement is just the for loop. Microsoft
+Visual C 6.0 disagrees and lets the definition "leak" out of the loop. Other
+compilers (like the GNU C++ compiler) conform to the C++ standard.
+Avoid writing code that relies on either behavior. The simplest
+rule is to never declare variables in the initialization spection of a for loop.
+If you do declare a variable there, make sure that it is not referenced outside
+of the for loop.
+
+++// This won't compile with Visual C++ (because it's broken) +int sum = 0; +for (int i = 0; i < 5; i++) + sum += i; +for (int i = 0; i < 5; i++) + sum += i * i; + +// This version won't compile with g++ (because it conforms to the standard) +int sum = 0; +for (int i = 0; i < 5; i++) + sum += i; +for (i = 0; i < 5; i++) + sum += i * i; + +// This version works on any compiler +int sum = 0; +int i; +for (i = 0; i < 5; i++) + sum += i; +for (i = 0; i < 5; i++) + sum += i * i; ++