Google+ Badge

Tuesday, October 9, 2012

Dialogs - gather data or do everything?

Should a dialog box simply gather data or should it do the processing on the data too? I have seen it done both ways in code with things intermixed in the same project. It gets very confusing when the two styles are mixed.

What exactly do I mean? A dialog box has a number of controls such as text fields, check boxes, lists etc. They gather information from the user as to what to do next such as printing a document, searching on criteria or choosing data to add to a table.

In the actual dialog box code you can also have it act upon that data and do the print job, perform the search or write data to the table but should you? I think the dialog should not perform the operation, it should only collect and validate the data.

Why do it that way? This keeps the processing separate from the UI. It allows you to run the print, search or table add in a headless manner, you don't need the dialog to appear to perform the operation.

So why do people chuck it all in the dialog? Because you already have all the data. Why pass it back and forth between the dialog and the operation? Boy that is annoying! Yes, there is more code involved to pull the data gathered out of the dialog. You can use separate method calls for each data item, a map or other collection type to get it back to the calling program. It really is not that big of a deal and it will save you a lot of time later when you need the operation separated from the UI. Lazy programming will come in bite you in the behind every time.

Don't cheat and make methods on the dialog object static so you can call them without invoking the dialog. That is a misuse of objects.

A lot of times the operation can take a lot of time. You want to show a busy state in the application and possibly run it on a background thread. Maybe the processing - such as printing - can run while the user does something else. If you leave this all in the dialog code that crazy UI sits on top of your app until processing is over. That is not very friendly. The dialog does not need to know about how the processing should be done. Is it quick and easy, does it take a long time and must complete before they move on to the next step or can the whole operation run in the background while the user moves on to the next task? Different data set sizes may need different processing actions. Don't lock all of this down in the dialog.