Methods on object orientated code
The code here shows three problems:
Methods on objects give a false sense to the programmer using the methods that they don’t need to think about the internals of an object.
Lack of separation of concerns
Difficulty of seeing how code is being used.
bool TRNCmap::addComponent(const TRNCconfig& Config){
COL_METHOD(TRNCmap::addComponent);
COL_VAR(Config);
if (m_NodeConfigMap.count(Config.m_Guid) > 0){
COL_TRC("Component already exists");
return false;
}
m_NodeConfigMap.addUnique(Config.m_Guid, Config); // hopefully shallow copy constructor works
m_NodeConnectionMap.clear();
rebuildConnectionMap();
COL_TRC("Added new component");
return true;
}
void TRNCmap::rebuildConnectionMap() {
COL_METHOD(TRNCmap::rebuildConnectionMap);
m_NodeConnectionMap.clear();
COLmap<COLstring, TRNCconfig>::iterator Itr;
for (Itr = m_NodeConfigMap.begin(); Itr != m_NodeConfigMap.end(); ++Itr) {
COLvector<COLstring>& Sources = Itr->second.m_Sources;
for (int i = 0; i < Sources.size(); ++i) {
m_NodeConnectionMap[Sources[i]].push_back(Itr->first);
}
}
}
Consider how this code works when:
We use it in the context of loading up N components when we start the product?
How can we easily see what parts of the code call these two methods?