| 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 | Good | 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.
A bigger problem is that misunderstandings will pile up as the time goes by. For example, a troubling student may take the following unfortunate path:
| Week 1 | I understand everything! Well, homework is a bit difficult, and my code doesn't work. Doesn't matter! I can just copy and paste the sample code and change some parts here and there; done! | He should have identified which part of his code was wrong and cleared his misunderstanding through debugging it, instead of just copying and pasting the sample code. |
| Week 2 | It's still OK. The instructor told us something that didn't make sense, but in general I understand the concepts. Hmm... homework and lab this week are hard. | His misunderstandings have piled up to some degree, but unfortunately, he doesn't know what he have missed. Just something is unclear to him. |
| Week 3 | This is a hard class, but it's still interesting. I can't finish one homework but it's fine. | It's not fine; he has a problem. He really needs to see his instructor. |
| ... | ||
| Week 6 | Cruel instructor, I know how to program, but the practicum is too hard for me even to touch!. Bad instructor, EI is useless. | Practicum was fair to most students. EI was painful to both him and the instructor. The student has so many misunderstandings, but he doesn't know what they are, and the instructor had to probe and identify his misunderstandings. |
There are some benefits to this approach:
As one more example, the online notes sometimes explain technical details. In this case, reading one paragraph may take even 5 minutes or more to work out all the details in your brain. Don't skip through the details. The devil is in the details.