Construct Messages Carefully

Although displaying error messages with simple strings is fairly foolproof, you need to be careful when constructing error message strings at run time. Specifically, you need to make sure that the string parameters are never blank. For example, consider the follow error-handling function:

 void DisplayGenericObjectError(HWND hParentWnd, LPCTSTR action,                                 LPCTSTR object) {    TCHAR buffer[LARGE_SIZE];    _stprintf(buffer, _T("Can't %s on %s."), action, object);    MessageBox(hParentWnd, buffer, AppName, MB_OK); } 

Suppose you call this function when both action and object are empty strings. This will result in the following error message:

Pretty useless, don't you think? As silly as it is, this type of mistake happens fairly often. Not surprisingly, the solution to this problem is to make sure it doesn't happen. For example, the above function could be changed to provide a generic error message whenever the action or object strings are empty:

 void DisplayGenericObjectError(HWND hParentWnd, LPCTSTR action,                                 LPCTSTR object, LPCTSTR genericError) {    TCHAR buffer[LARGE_SIZE];    if (*object != _T(`\0') && *action != _T(`\0'))       _stprintf(buffer, _T("Can't %s on %s."), action, object);    else       _tcscpy(buffer, genericError);    MessageBox(hParentWnd, buffer, AppName, MB_OK); } 

Alternatively, you could handle such an error by assigning generic names to the action or object:

 void DisplayGenericObjectError(HWND hParentWnd, LPCTSTR action,                                 LPCTSTR object) {    TCHAR buffer[LARGE_SIZE], objectBuf[LARGE_SIZE],           actionBuf[LARGE_SIZE];    if (*object != _T(`\0'))       _tcscpy(objectBuf, object);    else       _tcscpy(objectBuf, _T("unknown object"));    if (*action != _T(`\0'))       _tcscpy(actionBuf, action);    else       _tcscpy(actionBuf, _T("perform unknown action"));    _stprintf(buffer, _T("Can't %s on %s."), actionBuf, objectBuf);    MessageBox(hParentWnd, buffer, AppName, MB_OK); } 

Now if the parameters are empty strings, the error message is:

While the resulting error message isn't especially helpful, at least it is understandable. It also isn't as embarrassing.

TIP
Make sure that the string parameters to error message text constructed at run time aren't empty.



Developing User Interfaces for Microsoft Windows
Developing User Interfaces for Microsoft Windows
ISBN: 0735605866
EAN: 2147483647
Year: 2005
Pages: 334

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net