Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Using the if-then-else framework, Part 1

Code maintainable branching logic with the if-then-else framework

No one should have to read or maintain code shown in Listing 1 below. Code that includes nested ifs, as in this example, can be a maintenance nightmare. It is usually difficult to read and understand, and even more difficult to modify, which makes the code highly error-prone.

Listing 1. Sample nested-if code

void decideUrl() {
     DataBank.Region region = db.getRegion();
     double limit = db.getLimit();
     String id = db.getUserId();
     if(region.equals(DataBank.EAST_REGION)) {
         if(limit > db.LIMIT_THRESHOLD) {
             setUrl(EAST_PRIVILEGED);
         }
         else {
             setUrl(EAST_NOT_PRIVILEGED);
         }
     }
     else if(region.equals(DataBank.WEST_REGION)) {
         if(isMemberWestAlliance(id)) {
             if(limit > db.LIMIT_THRESHOLD) {
                 setUrl(WEST_MEMBER_PRIVILEGED);
             }
             else {
                 setUrl(WEST_MEMBER_NOT_PRIVILEGED);
             }
         }
         else {
             if(limit > db.LIMIT_THRESHOLD) {
                 setUrl(WEST_NONMEMBER_PRIVILEGED);
             }
             else {
                 setUrl(WEST_NONMEMBER_NOT_PRIVILEGED);
             }
         }
     }
     else {
         setUrl(OTHER_REGION);
     }
 }


TEXTBOX:

TEXTBOX_HEAD: Using the if-then-else framework: Read the whole series!



:END_TEXTBOX But what are you supposed to do when you need to write code that has branching logic? Often, you can sidestep the issue by making sure that lookup values are stored in a database. If the database solution is not an option, you can usually solve the problem by mimicking the database solution with Java Hashtables. But if you have to repeatedly implement this solution, with slightly different conditions and events, you will discover as I have, a burning need for a more general solution.

This article is about a little framework I have developed for writing code with branching logic without nested ifs. The framework resides in a single package and has a simple API that you must use. Using this framework, you can rewrite the code in Listing 1 in just a few lines (shown below), and encapsulate the conditions and consequences in a table that is easy to maintain.

void decideUrl() {
    try {
        Invoker inv = new ConcreteInvoker((Updateable)this);
        inv.execute();
    }
    catch(NestingTooDeepException ntde) {}
    catch(IllegalExpressionException iee) {}
    catch(RuleNotFoundException rnfe) {}
    catch(DataNotFoundException dnfe) {}
}


My discussion of this framework will span three issues of JavaWorld. The first two parts of the series are devoted to an explanation of how to use the framework. As I develop my ideas, I will show you how to code the branching logic of Listing 1 without nested ifs. After reading these first two parts, you should easily be able use the framework in your own work -- simply import the logic package (see Resources) and follow the steps described in the article. In the third part of the series, I will explain in greater detail the design and implementation of the framework, and will discuss refinements that you can introduce if desired. The seed ideas for this framework are among the many profound design secrets I learned from Ali Arsanjani during informal discussions in 1998 (see Resources).

1 | 2 | 3 | 4 | 5 |  Next >
Resources
  • Recent work by Ali Arsanjani
  • "Service ProviderA Domain Pattern and Its Business Framework Implementation," presented to PloP '99, is available from the online proceedings at
    http://st-www.cs.uiuc.edu/~plop/plop99/proceedings/Arsanjani/provider3.pdf
  • "Rule ObjectA Pattern Language for Pluggable and Adaptive Business Rule Construction," submitted to KoalaPLoP 2000. This paper will appear in the conference proceedings at the conclusion of the conference on May 26, 2000. Look for it online
    http://www.bell-labs.com/topic/conferences/KoalaPLoP/
  • "Analysis, Design, and Implementation of Distributed Java Business Frameworks Using Domain Patterns," Proceedings of Tools '99 (IEEE Computer Society Press 1999), pp. 490-500.