Tuesday, February 14, 2012

assert(g_everything->isEvil())

I'm in development for 3 months now. I must learn I understood something important. Something that I learned long ago but understood only now. Global vars are evil.

I saw many books about it, but I did not understand. A well placed global variable is very convenient and there is no harm in it. This is what I though when the largest project I experienced was around 2000 lines of code.

If you think what I thought 3 months ago you will hopefully learn from this blog post.

Look at this function signature:
error_code_t loadItemFromDefaultDir(const std::string filename);

As you can see function loads item and returns error code. Where is the item?

if(loadItemFromDefaultDir("it42.item")!=NO_ERR) {
throw load_exception;
}
sub_item_t* mySubItem = g_everything->repository->collections->getSub("it42", MY_SUB_CODE);


You may think there is no problem here, and you are partially right. The problem begins when the item is loaded in one place but the function that uses it is in different module. And the loader does not check the error code. And the item name is not constant, but dynamically created string... You got the idea.

Sometimes you write code like this and it does not work for your argument, but works for any other argument.
convertAndNormilize(&myConvertibleString);

You spend much time looking at the code and finally veteran developer feels pity on you and explains that the string you pass is defined in g_everything->exceptions->conversion_exempt_strings and you must first call
g_everything->exceptions->override_convert_exc_when_norm(myConvertibleString)

No comments: