At the top of your C file, write a comment with your name, etc., in the following form:
This information is not strictly necessary, since your files are already identified by their names and the repository they reside in. Nevertheless, the redundancy is a helpful convenience for us when we are browsing and/or grading your work. It also gets you used to writing such a header at the top of every file./* Jane Doe, jdoe */ /* CS152, Winter 2016 */ /* hw 1 */
Prior to each function, there needs to be a function header in the following form:
/* function name * purpose: (write the purpose) * inputs: * type name - what it is * ... each variable * outputs: * type - what it is */
A single-line comment is required for every control structure, including loops, if statements, recursive calls, and switch statements. They should state the code in English. For example, prior to a for loop, the comment should indicate what we are iterating over. For example, for each score in the array or for each student in the class. Prior to an if statement, it should state what we are checking for. A recursive call should be the english equivalent of describing what the smaller case is (like summing all but one of the elements of the array).
Comments, where they occur, should be precise and informative. Do not comment the obvious:
/* I'm adding b and c, and naming it "a"! */ <== not helpful int a = b + c; /* Summing number of birds and crocodiles */ <== much more helpful int a = b + c;
If a comment spans multiple lines, then you use the following format:
Be careful: C syntax does not support nested comments./* begin comment: slash star * middle lines start with space, star * the last line is a space, then end comment: star slash */
As a rule, do not write more than one statement on a line.
Your code should be no more than 80 columns wide. You can display the current column number in vim with the command
:set ruler
You will also want to issue the following two commands to vim to make the environment more pleasant to work in:
The first command turns on syntax coloring, and the second turns on line numbering. They're both very helpful.:syntax on :set number
Curly braces surrounding function bodies are placed on their own lines.
int my_example_function(int degrees) { }
Otherwise, you can choose one of two curly braces conventions. Whichever you choose, you must be consistent throughout your code. Choice 1: Open curly braces appear at the end of the first line on which they appear, as shown in the following example.
if (some_boolean_expression) { return 0; } else { return 7; }
Choice 2: Open curly braces appear on their own lines, and they are lined up with the beginning of the line with which they are associated.
if (some_boolean_expression) { return 0; } else { return 7; }
Loops follow the same conventions as if statements.
The indentation for switch statements is as follows:
switch (var) { case (0): single line here, with break; case (1): multiple lines are lined up here; then you have a code block; and then end with break; case (2): case (3): two cases leading to the same code are stacked. default: default case is always last }
Indentation: You must indent each control structure, as shown in class.
Now and throughout the quarter, consult KnR or class lecture notes for code layout standards.You need to choose good variable names.
int a = b + c/* not good names */
int animals = birds + crocs;/* very good names */
You may have one-letter variables as loop counters in for loops. When no other naming convention is present, loops use the counter i. If you have nested loops, then the nesting is i, j, k, l, etc.
Another acceptable convention is to use one-letter abbreviations of the thing you're iterating over, as shown below:int i, j, k; for(i=0;i
int r, c; // for each row for(r=0;r
Remember you need to add and commit every file to your subversion repository. If, for any reason, you do not have access to your subversion repository today, please save your work (carefully) somewhere you can get to it later, and add and commit it as soon as you can. Please note that the CSIL Macintoshes are wiped clean every night, so saving files on them is not an option.