|
C M S C 2 1 4 C o m p u t e r S c i e n c e I I F a l l 2 0 0 2 |
The goal of this exercise is to make sure you practice good style. It should only take you several hours to finish this task.
This part is worth about 5 points of your total project grade, so you can decide its importance (i.e., you can decide if you want to skip this or do it, since you know how much it's worth).
As a testimonial, one former student said that his code was much easier to read once he began following some of the style tips in this exercise. Before, he never worried that much about his coding style.
This contains a program that determines if one set is a subset of another. There are a list of tasks you're expected to follow, to improve the readability of the file.
When you're done, submit the modified StyleExercise.cpp with your entire tar file.
// ************************************************************ // Name: Dion Blazakis // Filename: StyleExercise.cpp // Project: 1 // // Login ID: bt214010 // Section: 0101 // TA: Dave Levin // Date Due: Sept 3, 2002 // Date written: Sept 1, 2002 // Estimated Time: About 2 hours // // Indentation Style: GNU // // Style exercise. // ************************************************************The header should contain the following information:
All files (.h and .cpp) should have this information at the top.
Students often complain that it's tedious to write this header for each and every file. However, there's an easy way to avoid all the typing. Create a file that just contains the header above (you could call it, say, header.file) and copy it whenever you start a new file. You will find it saves time.
if ( num > 3 ) { // <-- open brace
count++; // body indented in
num--; // body indented in
} // <-- close brace
Features
if ( num > 3 )
{ // <-- open brace (not indented in)
count++; // body indented in
num--; // body indented in
} // <-- close brace (not indented in)
Features
if ( num > 3 )
{ // <-- open brace
count++; // body indented, aligns with brace
num--; // body indented, aligns with brace
} // <-- close brace
Features
if ( num > 3 )
{ // <-- open brace (indented in)
count++; // body indented in some more
num--; // body indented in some more
} // <-- close brace
Features
After Estimated Time, write the following:
// Estimated Time: About 2 hours
//
// Indentation Style: K&R
emacs supports K&R and GNU style with the fewest problems,
but you can pick any of the four styles. Try to use one of the four
styles, even if that's not what you do normally.
int main()
{
int x = 3;
if ( x == 3 )
cout << "great" << endl;
x++;
return 0;
}
Reindent to make it look like:
int main()
{
int x = 3;
if ( x == 3 )
cout << "great" << endl;
x++;
return 0;
}
The body of the function is indented in at least 2 spaces, and
lines up. This makes the body appear a part of the function.
emacs normally does this, if you hit TAB in the line (at least,
when you edit code in a file ending in .cpp or .cc).
if (x==y) {
x=x+1;
cout<<"ok"<<endl;
}
Write
if (x == y) { // space added before and after ==
x = x + 1; // space added before and after = and +
cout << "ok" << endl; // space added before and after <<
}
Binary operators include:
if (x == y) {
x = x + 1;
cout << "ok" << endl;
}
Write
if (x == y) {
x = x + 1 ; // add a space before semicolon
cout << "ok" << endl ; // add a space before semicolon
}
This is an unusual style tip, but I think it's actually
useful. I'm sure you've never seen this style idea anywhere.
So why do it? It's often difficult to see semicolons, because they are so close to the statement itself. This makes errors with semicolons hard to debug. You can literally stare at the code for a long time and not notice a missing semicolon. By adding a space, it stands out more, and should be easier to notice when it's missing.
int x=2,y=3,z=4,w=5;
z=foo(x,y);
Write
int x = 2, y = 3, z = 4, w = 5 ; // single space after comma
w = foo(x, y, z) ; // single space after comma
Again, this makes the arguments of a function easier to read,
and makes the declaration easier to read, because it's
less cluttered.
w = foo(x, y, z) ; // computes averageLonger comments should precede the body of code Having a // as the first and last line make the comment stand out more.
//
// Finds the smallest value in the list, and updates it.
//
for ( int i = 0; i < size; i++ )
{
// body
}
There are other forms of documentating style. In particular,
Java has a tool called "javadoc" for extracting webpages from
comments. While you do not have to do this here, you should
read about it here
foo(x, y, z) ; // FineOr one space just after the open parenthesis and one space just before the close parenthesis
foo( x, y, z ) ; // FineBut not,
foo(x, y, z ) ; // NOT OK, space before close, but not after openNor,
foo( x, y, z) ; // NOT OK, space after open, but not before closeThe last two versions do not create symmetric spacing with the parentheses.
When you compile the program, and input redirect this file,
the output should be:
First set IS subset of second set
When you compile the program, and input redirect this file,
the output should be:
First set is NOT subset of second set
Make sure your code compiles after making all the changes.
|
See the class syllabus for policies concerning email Last Modified: Fri Sep 13 20:11:45 EDT 2002 |
|
|
|
|
|