Note on Programming Practice
I have been attending 2 code review sessions since coming back from Tawau. Here's what I've learned.
- Put comment into the source code, i.e. what it does, and which function is calling it.
- Exception handling on HANDLE that could not be closed properly.
- Code that repeats several times can be put into 1 function. This way, it will be centralized and easy to debug.
- Pointer must be deleted to avoid memory leak.
- std:vector can be used instead of fix size array.
- It is better not to use query all statement to query everything out from the database.
- strncpy and strncat are safer way to manipulate string.
- Construct an exception class to throw error instead of throwing number.
- Don't use too much pointer if can.
- Database connection must be caught and handled.
- Have constant localized at 1 place. This way, it makes it more easy to manage.
- Declare only the functions that need to be interfaced with other class as public; otherwise the rest should be private.
- Use const keyword to avoid a variable being changed by a function.
- Hide the field, and use external method to access the field. This is to avoid the private field being exposed.
- Make the code portable.
- The unwanted/commented code should be deleted once it is confirmed.
- Use of std:auto_ptr to avoid memory leak.
- Have a fixed variable declared outside of the function loop instead of calling the function within the loop. It is a better practice to reduce the number of call time to the function that returns i.e. array size.
- 1 method/function should not exit 100 lines of code.
- There should only be at least 10 if...else... statement within a method.
- Try not to use new everywhere.
- Prevent too many nested if... else... statements if possible.
Comments