Using good style is is a habit that every good programmer should strive for. The programs you write in this class will be graded for style as well as correctness. In particular, I will be looking at the following items.
You may choose how deeply to indent your code, as long as each indentation is at least 2 spaces and the indentation is consistent. Here are two blocks of code with different indentation depths, both of which would be acceptable:
4 spaces:
2 spaces:
It is not acceptable to mix indentation levels like this:
Curly braces must be lined up with the beginning of the block that they complete. The following is not acceptable:
Put a space on either side of operators. This makes it much easier to tell where variables end and operators begin.
Do not do this:
Putting a space after keywords like for
, if
, etc. is optional, but be consistent. Same goes for a space between a closing parenthesis and an opening curly brace. Both of these are acceptable:
and:
But inconsistency is not ok:
You may place the opening curly braces on the next line if you wish:
But you must be consistent throughout your program.
Some rules for variable names:
this_is_snake_case
thisIsCamelCase
i
, j
, and k
for loop variables is common, and many problems have an abstract quantity n
or x
, but choose a more descriptive name outside of those casees.col
for column) but often writing out the entire word is clearer.letters
but instead use letter_count
.