Jan/081
Do not use magic numbers!
Well let’s take an example :
int i;
for (i = 0; i < 128; i++)
{
switch(ship[i].type)
{
case 0 : ship[i].speed = 100.0f; break;
//…
}
}
Got the point? Those numbers there doesn’t tell a thing about them, that’s why it is good practise to get rid of the numbers and replace them with something more understandable. Check the modified version :
enum ShipType
{
SHIP_TYPE_CARRIER=0,
//…
SHIP_TYPE_MAX
};
#define SHIP_SPEED_SLOW 100.0f
#define NUM_SHIPS 128
int i
for (i = 0; i < NUM_SHIPS; i++)
{
switch(ship[i].type)
{
case SHIP_TYPE_CARRIER : ship[i].speed = SHIP_SPEED_SLOW; break;
//…
default : assert(ship[i].type < SHIP_TYPE_MAX); break;
}
}
That’s better right?
It is very important to keep your code readable!
By the way if anybody knows a good way to display code, please let me know as this sucks.
Jan/080
Warnings
This is simple, but very important and usually forgotten.
Warnings are there for a reason, so get rid of them!
And setup your compiler right :
- Change the warning level to maximum.
- Even better : treat warnings as errors.
Jan/082
Function cohesion
High cohesion means that you code is reusable, reliable, etc. Now quess what low cohesion means?
Best kind of function cohesion is when a routine performs only one operation without using any other routines. Why does this matter? It is easier to maintain than a routine that does 100 things and contains 1000 lines. Compare that routine to a routine that does 1 thing and contains only 10 lines.
There has been research statistic about this, and the lowest amount of bugs was in small routines with high cohesion. But that is obvious, so everytime you create a new function, try to remember the rule of doing only one thing.
Try to keep things as simple as possible.
Jan/080
Launching new website!
So I decided to get rid of the old Dawn Bringer – website and create a new very simple black and white look. I’m pretty sure this will change in the future so just try to live with it as long as I get somebody to redesign it.
I’ll be mostly posting tips & tricks about programming, but you will be seeing other ‘interesting’ stuff also.
First of all I’ll start collecting some of my older tips & tricks to here. If you have any questions or you just want to complain feel free to contact me (that information can be found from the About – page).
You can also help me by sending me a few dollars or whatever you like through the Money Machine – button, so that I can buy new toys for myself and for my daughter.