An Example:

Suppose you had software which matches buyers and sellers, and new users are created via a ‘new user’ wizard[1]. Let’s say the wizard has 4 pages for Basic User Info, Review, Processing, and Completion Status Report. And there are 5 buttons; Cancel, Previous, Next, Run, and Finish.

Cancel is displayed from the Basic User Info, Review, and Processing pages.
Previous is displayed from the Review page.
Next is displayed from the Basic User Info page.
Run is displayed from the Review page.
Finish is displayed from the Completion Status Report page.

The Problem

It may not seem like that big of a deal when you have only a few controls and a very simple (or no) workflow. Actually, you may argue, managing visual display in the events may even seem like the most efficient strategy. After all, you will never display, disable, or otherwise needlessly manipulate a control, but as your application grows more complex, this approach can leave you tearing your hair out[3]. This is especially true when you have multiple execution paths leading to the same event handlers with different state.

What if you received a change request for the above requirements, where additional info is required for buyers or sellers, resulting in a new wizard page for each, and in addition to that, business sellers require a page to gather Tax Information?

Notice how these changes lead to different execution paths all arriving at the Review page. The question then, is; where does the back button on the Review page send the user? The back button requires logic to know which wizard page to display.

It’s not difficult to imagine the need for logic to be added in more than one place[4], which can result in your display being processed differently from every event. Not only does this lead to repeated code, but can also create bugs where buttons do/don’t display where they should.

Eventually, as this logic grows increasingly complex, simple, 5 minute, change requests will eventually take you hours to determine there won’t be any side effects.

At this point, developers usually find …

Inadequate Solutions

So how do people manage this? I mean, beyond having the logic at the control level.

Well, you can put controls for different states on their own panel for their states. So in the example above, you could add the buttons directly onto the wizard page control[5]. This would ensure the correct buttons, for the panel, are displayed, but it doesn’t solve the ‘Back’ button logic of which page to display.

Or you could add some kind of control grouping mechanism, like a collection of the controls, to display or hide all controls at once. The problem with this, is what happens when you have functionality which will enable/disable a specific control, or do something else? Do you add another grouping or another function to manipulate your control group, or do you have that one function do enabling/disabling as well? What happens if the enabling/disabling logic is different from the display logic? You also have the problem of one control being in multiple groups; will your groupings need to be called in the order of hide, then show, just to get the control that’s in both groups to display? And if you do, will you let the control flicker? Or will you add extra functionality to prevent specific controls from hiding in the first place, so it won’t flicker?

Or maybe you can make a centralized function to manage the controls where the display is based on the passed in parameters. Well this is very close, but no cigar. Not only are the parameters needless, as I’ll show later, but this won’t completely remove logic from the events and can needlessly increase logic in that function.

The solution most have preferred is to make screens as minimal as possible, with a new screen for each piece of functionality. This is often effective, at the cost of usability, but sometimes has its own problems like managing state while gathering data from multiple screens to persist it all in one shot. Not even to mention that this strategy would be ineffective in our example. Besides, in my experience, you can never really get away from at least some display manipulation.

This is a small example; imagine having a screen with 50 controls, where many of them change state. Imagine how difficult it would be to manage the display this way.

Hopefully you can see how this can turn into a major pain in the butt.

Well, the good news is this problem has been solved …. Solved a long time ago actually[6].

The solution is ….

The UpdateUI() Method

It’s pretty simple really; just create a single function for the entire page, view, form, or screen (whatever it’s called in your technology). All this function does, is manage the control display. Then call this function at the end of every event, after all data manipulation.

It’s pretty simple; there are just …

5 Rules for the UpdateUI() method

1. Fast – Don’t do anything which isn’t fast. The state data driving the display decisions should already be calculated.

2. No parameters – You should be able to make all decisions about display using the current screens state.

3. No side effects – Don’t manipulate data within this method. Not only can it slow it down, but it can also lead to inconsistent display evaluations.

4. Manage each control individually – Don’t hide all controls, then show the controls you want displayed. This causes flicker and adds unnecessary complexity.

5. Call it last – call this function at the end of every event.

Code Sample

Here’s a little sample of what the wizard UpdateUI() code might look like.

Unfortunately, if your screen has a lot of controls, you may end up with a lot of these properties. Because the state used to make the display decisions is usually in that screens class, it’s difficult to move these expressions into a separate class. Or at least it is difficult to make a generalized statement about how to do it.

Where to use

This technique can be used in every GUI language or technology I’ve ever worked with. I’ve used this strategy in Classic VB, C++ (MFC[7], Win32), Classic ASP, PHP, JavaScript, ASP.NET webforms[8] , etc…

Costs

The cost to managing your display this way is a slight increase in computing power required to calculate every display decision on every event. However, in reality, if your UpdateUI() function causes a performance issue that’s noticeable, you’ve probably missed one of the rules stated above.

In my opinion, the increased simplicity, and cleanliness of you code, more than makes up for the slight increase in processing.

Conclusion

The UpdateUI() method is an efficient and effective way to keep your display manipulation code clean & bug free. Even if your screen is relatively simple, you still benefit from having all your code in one place, and if it does grow in complexity, it will be manageable. This technique has saved me countless hours of frustration.

[5] Wizard page controls are on their own panel of course, I mean we’ve all definitely learned that haven’t we?

[6] MFC had UpdateUI() as a virtual method on its main window class in the mid-1990s. Fortunately, this was one of the only new paradigms Microsoft has ever added which did not undo the solution I came up with in the last paradigm

[7] The name actually comes from the CView::UpdateUI() method in MFC which was a core method for all UI related activity, before MFC I was calling it EnableCtrls().

[8] In ASP.NET WebForms, this function is called Page.PreRender. However, this solution isn’t the greatest, since any control with an event needs to be populated in Page.OnInit just to wire up events properly.

2 Responses to “The UI programmers (not so) secret weapon”

Nice post John. Having a single well-known call to update your UI is certainly a big advantage. You note that you’ve used this approach in JavaScript as well. Have you employed it concurrently on the server and client (for example, to facilitate some specifically client-side-only UI update logic), or do you typically decide that the control will either be “all server” or “all client”?

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

About Me

Senior Software Consultant working in the Toronto area. I specialize in Microsoft technologies and have a long history of taking software development projects from idea, through development and implementation, and years of maintenance.
Always looking for interesting projects.