Friday, September 24, 2010

Condition Checking Inside Code

Many code blocks has condition checks during execution. If the check is nested deep inside some class structure, it is not easy to determine which containing class the check belongs to. Consider the possibility of shifting the checking outside execution, such as a pre-condition check during system initialization, or so on.

Consider the following.

bool Controller::execute(char* data_) {
    if(data_==NULL) {
        return false ;
    }
    if(_manager==NULL) {
        return false ;
    }
    Object* obj = _manager->getObject(data_) ;
    obj->process(data_) ;
    return true ;
}
There are two checks. The check if(_manager==NULL) can be placed outside the method since _manager is independent of the execution and should be set beforehand anyway.

No comments:

Post a Comment