Wednesday 19 April 2017

Only use ShowMessage() function for debugging

I always use the ShowMessage function for debugging while doing development and always exclude it from release code for the following reasons:


  1. MessageDlg or MessageBox functions are much better as a popup dialog than ShowMessage, they have more options and the presentation is better giving the user a visual icon categorising the meaning of the popup.
  2. It is easy to search through all the code and check there is no 'ShowMessage' operational before checking in the code. I had a customer call me once saying they have a popup (ShowMessage) which just says 'Hello', this was because the developer forgot to remove or comment out the ShowMessage before checking in.
  3. Because there is no icon with a ShowMessage, some testers and end users assume the popup is an error dialog. I have had the same but to a lesser extent with MessageDlg, the icon helps to show when it is information, warning or confirmation.
Some developers seem to use ShowMessage to quickly display information to the user, they are using ShowMessage because they think it is quicker to use, but in GExperts there is a Message Dialog tool that creates the code based on the options the developer sets.

There is the argument not to use these popup at all, and in some applications they cause issues. I think that popups can be annoying to the user if used too much, but I do not have a problem with them if they are used sparingly and they do not cause problems with the operation of the application.

I imagine some developers might say you should never use ShowMessage to debug and use breakpoints while debugging, which in most cases is the true, but there are occasions when some testing by the developer might be done in a non-development environment, in which case you cannot use the debugger. 

3 comments:

  1. Never use ShowMessage for debugging - use logging. There are numerous solutions for that.

    ReplyDelete
    Replies
    1. One advantage of ShowMessage is point 2, you can easily search the code quickly for all instances of ShowMessage and deal with them, I completely remove them. Logging raises other questions, but if it is used for debugging, in the code it should be highlighted, with a comment, todo or even placed in a conditional define.

      There are advantages with logging include additional information being added by the existing logging code, logs files are store on the file system so can easily be copied and sent to the developer. One area that needs to be addressed relates to my point 2, what happens if the developer leaves some logging code in there which should not be there for obvious security reasons? One answer is that logging needs to be encrypted, in-fact this really is a must nowadays if the logs are being copied and sent by email or other file transfer methods.

      Delete
  2. I would not emphase this method as perfect solution either.
    Stefan is absolutely right.

    The ShowMessage is the least attractive debug option IMHO.
    What I do in some smaller projects is to embed a hidden label, with a timed fallback system.
    So that I can show debug messages (even the end-user can activate the debug mode with a secret key from hotline assistance), and then show a debug statusLine for a short time.
    The debug mode is not persistant, and will be OFF in next startup.

    This poor mans logger is even better than ShowMessage, since it doesn't interfear normal operation by popups too much.

    Rollo

    ReplyDelete