/
Using a hidden global pointer for functions

Using a hidden global pointer for functions

This is an example of a coding practice to avoid. There is a global pointer to an object CFGmap which is used by the functions in this cpp file to avoid the need to pass the pointer to this object when the function calls are made:

CFGmap* pComponentMap; void TRNCdashboardInit(CFGmap* pMap){ COL_FUNCTION(TRNCdashboardInit); pComponentMap = pMap; COLvar File; FILserializeLoadConfig(&File, CDIRcacheDir(CDIRsubDir::ROOT), "ComponentState.json"); COLmap<COLstring, CFGconfig>& Map = pComponentMap->nodeConfigMap(); COLmap<COLstring, CFGconfig>::iterator it;; for(it = Map.begin(); it != Map.end(); it++){ COL_VAR(it->first); it->second.m_ErrorCount = File[it->first]["error_count"]; it->second.m_QueueCount = File[it->first]["queue_count"]; } } void TRNCdashboardSaveCounts(){ COL_FUNCTION(TRNCdashboardSaveCounts); COLvar ToSave; const COLmap<COLstring, CFGconfig>& Map = pComponentMap->nodeConfigMap(); COLmap<COLstring, CFGconfig>::const_iterator it;; for(it = Map.cbegin(); it != Map.cend(); it++){ COL_VAR(it->first); ToSave[it->first]["error_count"] = it->second.m_ErrorCount; ToSave[it->first]["queue_count"] = it->second.m_QueueCount; } COL_TRC(ToSave); FILserializeSaveConfig(ToSave, CDIRcacheDir(CDIRsubDir::ROOT), "ComponentState.json"); } void TRNCdashboardIncrementErrorCount (const COLstring& ComponentGuid, int Count){ COL_FUNCTION(TRNCdashboardIncrementErrorCount); CFGconfig* pNode = pComponentMap->node(ComponentGuid); if(pNode) { pNode->m_ErrorCount = pNode->m_ErrorCount + Count; } }

What makes this less than ideal?

Related content

Methods on object orientated code
Methods on object orientated code
More like this
Static keyword - it's multiple meanings in C++
Static keyword - it's multiple meanings in C++
More like this
Const References for Inputs and Non const Pointers for Outputs
Const References for Inputs and Non const Pointers for Outputs
More like this
Built in C++ namespaces make it hard to refactor code
Built in C++ namespaces make it hard to refactor code
More like this
Predeclarations instead of #include
Predeclarations instead of #include
More like this
Red flag - Helper functions which alter what they do with an input parameter
Red flag - Helper functions which alter what they do with an input parameter
More like this