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 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. A properly configured VSCode installation can be setup to make a tab automatically insert 4 spaces.
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).
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: Remove space between line content and curl braces
#include <stdio.h>
int main(){
for (int x = 0; x < 10; x++){
if (x % 2 == 0){
printf("%i\n", x);
}
}
return 0;
}
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;
}
Curly braces (blocks) on the following line is not wrong, it just is not the style we will be using for class. This is a perfect example of following the coding style guidelines of whatever company, employer, or project your are working on/for. In practice, consistency is more important than debates over code style.
my_function
double calculate_interest() {...}
double interest() {...}
const
#pragma once
?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