3

What would be the way to design a system which has logic that may change year wise? There is an application which conducts online exams.

There are five questions for a particular subject. The questions may (or may not) change year wise.

As per my current design, the questions in database are stored year wise. There are some year specific code logic as well. In order to enable the application for another year, the year specific database records and code will be copied or duplicated.

How to avoid this code duplication?

  • What's the cost to make the annual updates to the code and questions? It may be that a general solution would require too many years to pay for the added complexity. Then again, perhaps not. – Dan Pichelman Nov 8 '13 at 17:10
  • @Dan The cost as well as the code base is very huge if the system has to deal with insurance forms or tax forms instead of questions. – aravind Nov 8 '13 at 17:16
  • When you copy the code, does this end up in some duplicated need for maintenance? Or do you just create a new version of the code, while the old version is freezed? The latter form of "code duplication" is not much of a problem. – Doc Brown Nov 8 '13 at 18:55
  • @Doc - No, the old version is not freezed – aravind Nov 9 '13 at 2:29
1

Actually, I think the techniques for avoiding code duplication are the same as in any other system. For example, think about a function

CalcTax2013(){...};

and let's assume you have to duplicate that function for the next year (perhaps with some changes)

CalcTax2014(){...};

then you should immediately refactor all common parts out to a function used from both of them. Of course, you should have written some automatic tests before that to make sure refactoring does not break anything.

There could be of course a different situation: when CalcTax2013 does not have to be changed later any more, lets say for example from 01/01/2014. If that's the case, there is no need for any maintenance on that function, which makes the refactoring pointless.

If this does not apply to your question, please give a more elaborate example.

  • That make sense. Thanks! However, my another question is what would be a good approach (or design pattern) for the client to identify which functionality to use. Say TaxClient is my application and there are two classes TaxCalc2013 and TaxCalc2014, both inherited from TaxCalcBase. Each of these classes have its own version of Calculate() method. How could tax client discover and execute the correct object and method? – aravind Nov 9 '13 at 6:54
  • @aravind: that's the point where you need a so-called factory method (en.wikipedia.org/wiki/Factory_method_pattern). How to implement that factory efficiently depends somewhat on the programming language you are using, and which of your data controls the decision on the correct object and method. – Doc Brown Nov 9 '13 at 7:21
3

It seems you are on the right path. A better solution might be to adjust the database structure. If you store the questions and code in different tables, you would just need to point to the respective questions and code for a particular year.

Only new questions and code would need to be added to the database.

  • The code could not be stored in database. If the language is c#, how could the code stored in database executed? – aravind Nov 9 '13 at 2:41
3

I would say this is best solved by making sure that the code that is most likely to change is as decoupled as it can be. Try to implement the front end access though a facade pattern and have the classes implement an interface that is robust so that any changes will only affect small parts of the code and are easy to swap out.

1

The basic concept is to abstract out what changes.

If you have a set of logic that varies, consider the Strategy pattern. This involves a number of classes that all implement a single interface (or extend a single type, depending on what language you are using). The appropriate implementation is instantiated and injected into the calling object. This is frequently implemented using a dependency injection framework such as Spring.

For example, you might have your controlling class, something like Questionnaire, which for question 1, calls a method in the IQuestionOne interface. This interface is implemented by different classes depending on the year: QuestionOne2013 containing the 2013 logic and QuestionOne2014 containing the 2014 logic. You could then build a Questionaire for 2013 and have it reference the QuestionOne2013 implementation of IQuestionOne. Likewise, the Questionnaire for 2014 would use the QuestionTwo2014 implementation.

If the logic doesn't change, you can simply reuse the same implementation. So, perhaps question 2 (abstracted as the interface IQuestionTwo) has the same logic for both years--simply use the same implementation for either questionnaire.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.