I have to insert a lot of data points from a structure which looks like this:
The InsertData() function above is pusdocode but the point is that this function (or the like) returns true or false. If a false occurs anywhere it is time to abort and quite the operation. In real situation I have a lot of these InsertData() function and I need to check the value after each one. It makes the code look a little ugly and error check code/user prompt message is repetitive. Is there a way to make it better? How about I put the error checking as #define ?Code:boo result = false; result = InsertData( temperature); if (result == false) { AfxMessageBox("An error occured"); return; } result = InsertData( pressure ); if (result == false) { AfxMessageBox("An error occured"); return; } result = InsertData( location ); if (result == false) { AfxMessageBox("An error occured"); return; } result = InsertData( .... ); if (result == false) { AfxMessageBox("An error occured"); return; }
Sometime I split such operation in two functions to separate and avoid repeating the message everywhere in the code.Code:#define DO_ERROR_CHECK if (result == false) \ { \ AfxMessageBox("An error occured"); \ return; \ } // NOW... result = InsertData( temperature); DO_ERROR_CHECK // and so on InsertData(....) DO_ERROR_CHECK
any thoughts? How do you do it?Code:DoOperation() { result = InsertAllData(); if (result == false) AfxMessageBox("An error has occured"); } InsertAllData() { if (!InsertData( temperature ) ) return false; if (!InsertData( pressure ) ) return false; }




Reply With Quote