While it is important that programs you write function correctly, it is equally important that they be readable and maintainable. This is something that you do not just for yourself, but for others who will be working with or reading the code you write. Coding style guidelines can vary with employer and programming language, but for the purposes of this class we will follow a few simple guidelines.
In some programming languages like C we use indentation to help visualize nested blocks of code. Each indent should be at least 4 spaces:
DO: Indent with 4 Spaces
#include <stdio.h>
int main() {
for (int x = 0; x < 10; x++) {
if (x % 2 == 0) {
printf("%i\n", x);
}
}
return 0;
}
DON’T: Mix different indentation sizes
#include <stdio.h>
int main() {
for (int x = 0; x < 10; x++) {
if (x % 2 == 0) {
printf("%i\n", x);
}
}
return 0;
}
If you do not want to repeatedly press the spacebar, you can also use the tab
key for indentation. This will give you consistent indentation in your program.
To make it easier to read a statement you should have one space between variables, operators, and literals:
DO: Proper spacing inside statements
#include <stdio.h>
int main() {
for (int x = 0; x < 10; x++) {
if (x % 2 == 0) {
printf("%i\n", x);
}
}
return 0;
}
Note the spaces after the for and the if. There should only be parenthesis immediately after function (like the printf). It is not necessary to have spaces between the semicolons (;
).
DON’T: Statements with missing spacing
#include <stdio.h>
int main() {
for (int x=0; x<10; x++) {
if (x%2==0) {
printf("%i\n", x);
}
}
return 0;
}
DON’T: Use inconsistent spacing
#include <stdio.h>
int main() {
for (int x =0; x< 10; x++) {
if (x% 2 ==0) {
printf("%i\n",x);
}
}
return 0;
}
Curly braces are used to indicate the start and end of functions, loops, conditional, and blocks.
DO: Start curly braces on the same line a block begins
#include <stdio.h>
int main() {
for (int x = 0; x < 10; x++) {
if (x % 2 == 0) {
printf("%i\n", x);
}
}
return 0;
}
DO: Put a space between the curly brace and any content that preceeds it.
#include <stdio.h>
int main() {
for (int x = 0; x < 10; x++) {
if (x % 2 == 0) {
printf("%i\n", x);
}
}
return 0;
}
Note the spacing between the ending parenthesis and the curly brace at the end of the for and if conditions.
DON’T: Start your curly braces on the next line
#include <stdio.h>
int main()
{
for (int x = 0; x < 10; x++)
{
if (x % 2 == 0)
{
printf("%i\n", x);
}
}
return 0;
}
my_function
const
Function prototypes should have comments above them explaining:
Here is an example:
/**
* Create the reverse of a string inside another array.
*
* Parameters:
* original - the original string, which will remain unchanged
* reversed - another character array which will store the reversed string
*
* Return value:
* None, but the modified string is stored in the reversed parameter
*/
void reverse_string(const char original[], char reversed[]);
Note: It is NOT necessary to have the asterisks for each line.
my_var
MY_CONST_VAR
i
, j
, and k
as loop variables is common, and some problems have an abstract quantity n
or x
, but otherwise choose a more descriptive name.col
for column
) but often writing out the entire word is clearer.letters
but instead use letter_count
.monthy_pay
is better than mon_pay
or simply pay
.