views:

114

answers:

4

How do you keep track of the business rules in your application code?

Lets say we are designing an order entry system. One of the business rules might be:

If the stock level of an item drops below its reorder point, create a purchase order for that item to bring its stock level back up to the minimum.

So, in our code, we might have a method somewhere that checks for this:

function check_stock_level() {
  if (this.stock_level < this.reorder_level) {
    po = new PurchaseOrder();
    po.add_item(this.item_type, this.minimum_level - this.stock_level);
  }
}

So how would you mark up your code (with comments maybe?) or document your code with a third party system to cross reference your business rule to the code that makes it work? Especially, how would you do this with code that might not be contained in one simple method (like the above example) but might stretch across multiple methods or multiple files even?

Are there any tools out there to manage this?

I remember reading somewhere about how NASA programmers must fill out a form for every change they make to the code base to describe what they did and why - I imagine this type of system would make development slow down considerably but then when you need to fix a bug it must be much easier to go back and find out where to start looking.

+2  A: 

I would organize my code in such a way that related business rules are all in the same unit of code (class or file, depending on your tool) and separate from non-business rule code. A good file-wise organization would allow you to track changes to it using version control software, such as Subversion, Perforce, git, etc.

Parappa
+1  A: 

There are general or already "business" oriented rule engines out there.

For the MS .net environment, the Microsoft Business Rules Engine (BRE) in BizTalk Server seems to be the standard.

For the Java environment there are a few rule engines out there, see this overview.

Or you role your own:

For Java, you can document the rules in JavaDoc. Use an annotation to mark "business rules" (e.g. the method or maybe a whole class).

Then write a custom doclet to piece this info together.

More info on JavaDoc see http://java.sun.com/j2se/javadoc/

Gerd Klima
A: 

first, I wouldn't embed these kinds of rules into the "Stock" object as your code suggests.

There are many approaches to this...

but say this could be captured as MaintainMinimumStockLevelRule and it could be logically grouped into a location (folder / component / whatever)

Then it should be pretty easy to find.

Or you build a rules engine into your software and the rules are configurable :-)

Keith Nicholas
A: 

Use a business rules management system. Using code to manage business rules is like using code to manage data or content - possible but not generally very helpful. If you have a lot of rules, rules that are complex or interact in complex ways, rules that change a lot or rules that require business domain expertise to really understand then you should seriously consider a rules management environment to manage them. If you like commercial products there are many (InRule for .Net, FICO and IBM for .Net/Java/COBOL, Innovations, IDIOM and Corticon for Java) and Drools is a great open source solution. I wrote a blog post about this some time back over on ebizQ

James Taylor