Tuesday, June 21, 2011

Design decides on performance

In our legacy system, we found a stupid design decision. We provide the service of business suite for our customer.  The relationship between customer and business suite is stored in the user_busi_suite table. When the administrator of system want to delete the specific business suite, the program should check whether the business suite was subscribed by user at first.  If the business suite had been subscribed, it will popup the alert window to give the noticement.
Guess how to implement this function in the legacy system? It fetched the all user who subscribed this suite, then draw a dicision whether delete it depending on the size of user list:
List users = busiSuiteRepository.fetchSubsribersWith(suiteId);
if (users == null || users.size() == 0) {
    busiSuiteRepository.remove(suiteId);
}
Its implementation will effect on the performance seriously. The query of the user with suite is not necessary. We should better define the property for BusinessSuite which indicates whether it was subscribed by user. Like this:
public class BusinessSuite {
    private boolean subscriptionFlag = false;
    public boolean isSubscribed() {
        return subscriptionFlag;
    }
    public void subscribe() {
        subscriptionFlag = true;
    }
}

BusinessSuite bs = busiSuiteRepository.with(suiteId);
if (!bs.isSubscribed()) {
    busiSuiteRepository.remove(suiteId);
}
Don't need to query the list of user, we only get the value of subsription flag to draw a decision. That's easy!

No comments: