Bad Formatting | Good Formatting | |
#include <iostream> using namespace std ;int main(){int x,y =1;cin>>x;while(x>0 ){y=2*y;x--;}cout<< y<<endl;return(0);} |
#include <iostream> using namespace std; int main() { int x, y = 1; cin >> x; while(x > 0) { y = 2*y; x--; } cout << y<<endl; return(0); } |
Good | y = 2*y + x/y + 5; |
if (y >= 2*y - 5 && x*y <= 24) |
Bad | y=2*y+x/y+5; |
if (y>=2* y-5&&x * y<=24) |
Good | Goodish | Bad | Bad |
while (x < 3) { cout << x << ","; x++; } cout << endl; |
while (x < 3) { cout << x << ","; x++; } cout << endl; |
while (x < 3) { cout << x << ","; x++; } cout << endl; |
while (x < 3) { cout << x << ","; x++; } cout << endl; |
/*************************************************** Fahrenheit to Celsius Conversion ***************************************************/ #include <iostream> using namespace std; int main() { // Read temperature in Fahrenheit double Tf; cout << "Enter temperature in Fahrenheit: "; cin >> Tf; // Compute temperature in Celsius double Tc; Tc = (Tf - 32)*(5/9.0); // .0 forces division as double // Write temperature in Celsius cout << "That is " << Tc << " degrees Celsius." << endl; return 0; }
yardsPerCarry
vice ypc
for program readability. This must be balanced against
using names which are too long, which can obscure the code. (Fifteen
or so characters approaches the “too long” limit.)
It should be mentioned that this is most important for variables
that have large scope — i.e. they are visible and need to be
used either from different files or from widely separated lines in
the same file. The code example above seems to violate this rule,
but variables Tf
and Tc
only a appear, and
are only in scope, within less than a dozen lines of code, and in
the context of those few lines they are descriptive enough. Also,
because this is an example embedded in a document, compactness of
the code is very important.
Single letter variables or constants should not be used with the following exceptions. An exception to this rule is when it is common practice to identify something with a single letter. An example of this is the coordinate system (x, y, and z). A second exception occurs in the use of loop counter variables where it is common practice to use variables like i and j as counters in for loops.
Function names and variable names should begin with a lower case letter. An identifier consisting of multiple names SHALL have each name distinguished by making the first letter of each name part (after the first) upper case (e.g. yardsPerCarry) or by using underscores (yards_per_carry). The former method is strongly encouraged. Constants should be named in all upper case letters. Example:
const int PI = 3.14;It's generally good to avoid lower case letter ‘L’, or the letter ‘O’ in names unless they are part of normal words. This is to avoid confusion with the numbers 1 and 0. For example, is "
cell
" c- e -
1- ONE or c- e- 1-1?
Avoid names that differ only in case, look similar, or differ only slightly. For example, InputData, InData and DataInput will certainly be confusing if used in the same program.
Names of functions should reflect what they do (printArray), or what they return (getAge). Boolean variable names should sound like Yes/No things — "isEmpty" and "isFinished" are possible examples.