One goal of this course is to encourage you to write very readable and modifyable code. An important part of achieving this goal is to have enforced style rules for coding. You can read Googles style rules here Google C/C++ coding style rules. We could have you follow those, but they are very long and more focused on C++, so here is the short version we will enforce in this course. Note these rules are simplistic, but that is to make them easy to follow and enforce. Judgement is required to know when a function is still comprehensible, but forcing you to keep them small goes a long way towards achieving this goal.
I’ve heard so many programmers say, “I just want to get it working and I’ll clean it up later” This is a path to failure that everyone must learn for themselves. As your code gets longer and more complicated, it gets more difficult to understand and more difficult to get correct. Taking a little time up front to keep your functions small and clean ultimately helps you write programs faster with fewer bugs resulting in you spending less time and effort to get a correct program. You can always make your code cleaner and shorter by inventing and writing more functions to do work for you. These functions should be given a single purpose and their name should clearly describe what this purpose is. Avoid creating function names that are meaningless like helper_function_1(), part1(), step2(), etc.
Here are the rules:
All function bodies (even main) should be five lines of code or smaller. Each statement counts as one line. Each for/while/do-while/if/switch counts a minimum of two lines even if the statement nested inside of them is empty.
The following do not count even though they take a line
function prototype, which gives the function name, formal parameters, and return type,
Lines containing nothing more than a curly brace,
Here are some exceptions:
Variable declarations with initialization, given in a group at the start of a function, no matter how many, only count as one line.
e.g., the following only count as one line even though there are really three lines.
int i = binary_search(key, array);
int k = i / 2;
int n = 0;
do-while statements: do { S; } while (); counts as one plus the count of lines of S inside the loop. e.g, the following counts as two lines of code
do
{
ch = getchar();
} while (ch != EOF);
if_else statments: the final else on a line by itself does not count, e.g., the following counts as three lines
if (a < b)
{
mid = a;
}
else
{
mid = b;
}
switch statements: are very different and must be counted differently. Count a switch statement within a function as three lines toward the function they are contained within. Then separately, limit each case within the switch to 5 lines per case. Each break or return in the case counts as one line, but the case tags (ie., case ‘a’: default: ) (no matter how many) all together count as one towards the limit for the case to which they belong. At most one switch statement is allowed per function.
Any empty statement, such as a loop body that is empty, must still count as one line, e.g.
while (*s++ = *t++) ; // still counts as two because the empty body is one
We will allow you to have 2 exemption functions per program with a length-limit up to 7 lines. For programs submitted for grading up through Week 6, we will also allow a third exemption up to 9 lines. You must label each as your exemption with the header comment right before the function definition. /* EXCEEDING 5 LINES BY 2 */ . After Week 6, the 9 line exemption will no longer be allowed and you can have only two 7 line exemptions per program.
DO NOT use the conditional expression instead of if statement to try to reduce number of lines. Only use conditional expression when returning the value or asigning the value to a variable to be used later.
DO NOT use the comma operator to combine expressions onto one line to reduce line numbers. Only use comma operator in for loops when doing multiple things in one of the three fields, e.g.,
for (int i=0, j=0; i <10 && j < 40; i++, j++) {….}
Nested statements should be indented by 4 spaces (avoid using tabs as they are hard wired to 8).
Curly braces should be one per line and should align vertically with the start of the statement they belong to and with each other.
Do not put comments on closing curly braces intended to show which statement you are closing, such as
if ( a < b )
{
…
} /* if */
Choose good names descriptive of the purpose of each function, parameter, and variable. The most common C style is all lowercase with underscores separating words. Library functions will often abreviate words and not use underscore separators, e.g., isalnum(c) for is_alpha_numeric.
When a variable or parameter may be any char, int, string, double, or function, it is fine to use a single letter names like c, i, s, d, or f, but the name of the variable should be consistent with the type of the variable, e.g., don’t use c for an int or s for a double. i,j,k,n should be int, s,t should always be a string, c,ch a char, d,x,y,z a double, f,g,h a function or function pointer, and so on. It is also common to add a “p” to the end of a variable that is a pointer, e.g., int * ip;