Coursework Do's and Don'ts

C/C++ Connect 4

Last Update Unknown

No magic numbers! 

Make your code more readable by ensuring that special numbers are declared as constants to make their meaning explicit.


The 7 in int board[][7] as a function parameter is a magic number! Use a 'global' class to create a 'global' constant for this instead.


Fix every warning!

Warnings are cases where the compiler understands what your program is doing, and thinks that it's probably not doing what YOU want it to do. The compiler is pretty smart, so it's usually right.

You could use gcc -Wall -o variables variables.c to detect this.


Don't hardcode size of arrays!

Use variables which you could easily change later! If you wanted to change the number of rows for example on your board, you don't want to have to go through your entire code trying to find and replace all the times you've mentioned that array.


Only assign unsigned integers if necessary to do it!

i.e. You're in the range of numbers that won't fit into a signed int but will fit into a unsigned int and you don't want to promote to the next integer size

OR if you are doing bit shifting and taking your integer number and shifting all the bits left or right


Test your makefile!

Make sure to test your makefile. Makefiles are very fussy and have to be formatted perfectly to work. It's very easy to get wrong or just miss out one of your files! See my makefile guide for more info!


Remember the terminating null on strings!

Always keep in mind that the null character marks the end of data in C/C++!

You may have to deal with this when getting input from the user!


Remember dimensions of arrays must be one bigger than you want!

A connect 4 board should be: 

int board[8][7];

NOT int board[7][6];


Don't use namespaces in headers!

using namespace std;

is fine to use but please don't use it in a header file. Just use it in your .cpp files.


Be careful with pointers!

Use referencing (the ampersand &) instead where possible!


Naming Conventions!

Stick with the same naming convention throughout your code if possible. The recommended style is:


Constants: 

ALL_UPPER_SNAKE_CASE


Non-const Variables and Functions:

lowerCamelCase

lower_snake_case

duditiousMix_units