Magic numbers are numbers that are used in code where if someone had no prior experience to the code it would appear as if you are doing random operations to your variables. Take a look at the following code:
1 2 3
|
if(x > 10 && x < 20 && y > 15 && y < 30) {
//Do stuff
}
| |
It is fairly obvious that you are determining if a point is in a box, but what is the box? It is questions like this that rise when you use random numbers. Obviously it gets a lot worse and you start doing physics calculations replacing values you know with numeric literals.
To correct the above code, one could write:
1 2 3
|
if(x > OKButton.left && x < OKButton.right && y > OKButton.bottom && y < OKButton.top) {
//Do stuff
}
| |
This way the code is not ambiguous to whoever is reading it.
Weird naming conventions are similar. They are basically where you name objects in a way that is either not consistent, or is hard to read. There are several standard conventions in terms of case( camelCase, PascalCase, using_underscores, m_MemberVariable etc ) and lots of people will have naming rules where they might for example say all variables are camelCase and all functions are PascalCase. But it also means that any names you give should be clear, unambiguous and short (< 30 chars if possible).
Inconsistent return types literally means that you have several functions which do a similar job and hence should probably return the same thing, but they return different things for no apparent reason. For example:
1 2 3 4 5 6 7 8 9 10 11
|
bool operationA ( unsigned& out )
{
//Stuff
out = result;
return did_it_fail;
}
unsigned operationB()
{
//Stuff
return result;
}
| |
Both of these are valid. The first usually used where an operation may fail and hence the result is given in an out parameter and the second is usually used where an operation is guaranteed to succeed (or relies on exceptions instead). Problems arise when you mix the two for no good reason because it means you have to go back to the declaration of each function to be sure what it returns, rather than it being obvious.