Pages

Tuesday, January 4, 2011

Theorems Regarding Business Logic

In yesterday's Rigorous Definition of Business Logic, we saw that business logic can be defined in four orders:

  • First Order Business Logic is entities and attributes that users (or other agents) can save, and the security rules that govern read/write access to the entitites and attributes.
  • Second Order Business Logic is entities and attributes derived by rules and formulas, such as calculated values and history tables.
  • Third Order Business Logic are non-algorithmic compound operations (no structure or looping is required in expressing the solution), such as a month-end batch billing or, for the old-timers out there, a year-end general ledger roll-up.
  • Fourth Order Business Logic are algorithmic compound operations. These occur when the action of one step affects the input to future steps. One example is ERP Allocation.

A Case Study

The best way to see if these have any value is to cook up some theorems and examine them with an example. We will take a vastly simplified time billing system, in which employees enter time which is billed once/month to customers. We'll work out some details a little below.

Theorem 1: 1st and 2nd Order, Analysis

The first theorem we can derive from these definitions is that we should look at First and Second Order Schemas together during analysis. This is because:

  • First Order Business Logic is about entities and atrributes
  • Second Order Business Logic is about entities and attributes
  • Second Order Business Logic is about values generated from First Order values and, possibly, other Second Order values
  • Therefore, Second Order values are always expressed ultimately in terms of First Order values
  • Therefore, they should be analyzed together

To give the devil his due, ORM does this easily, because it ignores so much database theory (paying a large price in performance for doing so) and considers an entire row, with its first order and second order values together, as being part of one class. This is likely the foundation for the claims of ORM users that they experience productivity gains when using ORM. Since I usually do nothing but bash ORM, I hope this statement will be taken as utterly sincere.

Going the other way, database theorists and evangelists who adhere to full normalization can hobble an analysis effort by refusing to consider 2nd order because those values denormalize the database, so sometimes the worst of my own crowd will prevent analysis by trying to keep these out of the conversation. So, assuming I have not pissed off my own friends, let's keep going.

So let's look at our case study of the time billing system. By theorem 1, our analysis of entities and attributes should include both 1st and 2nd order schema, something like this:

 
 INVOICES
-----------
 invoiceid      2nd Order, a generated unique value
 date           2nd Order if always takes date of batch run
 customer       2nd Order, a consequence of this being an
                           aggregation of INVOICE_LINES
 total_amount   2nd Order, a sum from INVOICE_LINES
               
 INVOICE_LINES
---------------
 invoiceid      2nd order, copied from INVOICES
 customer         +-  All three are 2nd order, a consequence
 employee         |   of this being an aggregration of
 activity         +-  employee time entries
 rate           2nd order, taken from ACTIVITIES table
                           (not depicted)
 hours          2nd order, summed from time entries
 amount         2nd order, rate * hours
 
 TIME_ENTRIES
--------------
 employeeid     2nd order, assuming system forces this
                    value to be the employee making
                    the entry
 date           1st order, entered by employee
 customer       1st order, entered by employee
 activity       1st order, entered by employee
 hours          1st order, entered by employee

Now, considering how much of that is 2nd order, which is almost all of it, the theorem is not only supported by the definition, but ought to line up squarely with our experience. Who would want to try to analyze this and claim that all the 2nd order stuff should not be there?

Theorem 2: 1st and 2nd Order, Implementation

The second theorem we can derive from these definitions is that First and Second Order Business logic require separate implementation techniques. This is because:

  • First Order Business Logic is about user-supplied values
  • Second Order Business Logic is about generated values
  • Therefore, unlike things cannot be implemented with like tools.

Going back to the time entry example, let's zoom in on the lowest table, the TIME_ENTRIES. The employee entering her time must supply customer, date, activity, and hours, while the system forces the value of employeeid. This means that customer and activity must be validated in their respective tables, and hours must be checked for something like <= 24. But for employeeid the system provides the value out of its context. So the two kinds of values are processed in very unlike ways. It seems reasonable that our code would be simpler if it did not try to force both kinds of values down the same validation pipe.

Theorem 3: 2nd and 3rd Order, Conservation of Action

This theorem states that the sum of Second and Third Order Business Logic is fixed:

  • Second Order Business Logic is about generating entities and attributes by rules or formulas
  • Third Order Business Logic is coded compound creation of entities and attributes
  • Given that a particular set of requirements resolves to a finite set of actions that generate entities and values, then
  • The sum of Second Order and Third Order Business Logic is fixed.

In plain English, this means that the more Business Logic you can implement through 2nd Order declarative rules and formulas, the fewer processing routines you have to code. Or, if you prefer, the more processes you code, the fewer declarative rules about entitities and attributes you will have.

This theorem may be hard to compare to experience for verification because most of us are so used to thinking in terms of the batch billing as a process that we cannot imagine it being implemented any other way: how exactly am I suppose to implement batch billing declaratively?.

Let's go back to the schema above, where we can realize upon examination that the entirety of the batch billing "process" has been detailed in a 2nd Order Schema, if we could somehow add these facts to our CREATE TABLE commands the way we add keys, types, and constraints, batch billing would occur without the batch part.

Consider this. Imagine that a user enters a a TIME_ENTRY. The system checks for a matching EMPLOYEE/CUSTOMER/ACTIVITY row in INVOICE_DETAIL, and when it finds the row it updates the totals. But if it does not find one then it creates one! Creation of the INVOICE_DETAIL record causes the system to check for the existence of an invoice for that customer, and when it does not find one it creates it and initializes the totals. Subsequent time entries not only update the INVOICE_DETAIL rows but the INVOICE rows as well. If this were happening, there would be no batch billing at the end of the month because the invoices would all be sitting there ready to go when the last time entry was made.

By the way, I coded something that does this in a pretty straight-forward way a few years ago, meaning you could skip the batch billing process and add a few details to a schema that would cause the database to behave exactly as described above. Although the the format for specifying these extra features was easy enough (so it seemed to me as the author), it seemed the conceptual shift of thinking that it required of people was far larger than I initially and naively imagined. Nevertheless, I toil forward, and that is the core idea behind my Triangulum project.

Observation: There Will Be Code

This is not so much a theorem as an observation. This observation is that if your application requires Fourth Order Business Logic then somebody is going to code something somewhere.

An anonymous reader pointed out in the comments to Part 2 that Oracle's MODEL clause may work in some cases. I would assume so, but I would also assume that reality can create complicated Fourth Order cases faster than SQL can evolve. Maybe.

But anyway, the real observation here is is that no modern language, either app level or SQL flavor, can express an algorithm declaratively. In other words, no combination of keys, constraints, calculations and derivations, and no known combination of advanced SQL functions and clauses will express an ERP Allocation routine or a Magazine Regulation routine. So you have to code it. This may not always be true, but I think it is true now.

This is in contrast to the example given in the previous section about the fixed total of 2nd and 3rd Order Logic. Unlike that example, you cannot provide enough 2nd order wizardry to eliminate fourth order. (well ok maybe you can, but I haven't figured it out yet myself and have never heard that anybody else is even trying. The trick would be to have a table that you truncate and insert a single row into, a trigger would fire that would know how to generate the next INSERT, generating a cascade. Of course, since this happens in a transaction, if you end up generating 100,000 inserts this might be a bad idea ha ha.)

Theorem 5: Second Order Tools Reduce Code

This theorem rests on the acceptance of an observation, that using meta-data repositories, or data dictionaries, is easier than coding. If that does not hold true, then this theorem does not hold true. But if that observation (my own observation, admittedly) does hold true, then:

  • By Theorem 3, the sum of 2nd and 3rd order logic is fixed
  • By observation, using meta-data that manages schema requires less time than coding,
  • By Theorem 1, 2nd order is analyzed and specified as schema
  • Then it is desirable to specify as much business logic as possible as 2nd order schema, reducing and possibly eliminating manual coding of Third Order programs.

Again we go back to the batch billing example. Is it possible to convert it all to 2nd Order as described above. Well yes it is, because I've done it. The trick is an extremely counter-intuitive modification to a foreign key that causes a failure to actually generate the parent row that would let the key succeed. To find out more about this, check out Triangulum (not ready for prime time as of this writing).

Conclusions

The major conclusion in all of this is that anlaysis and design should begin with First and Second Order Business Logic, which means working out schemas, both the user-supplied values and the system-supplied values.

When that is done, what we often call "processes" are layered on top of this.

Tomorrow we will see part 4 of 4, examining the business logic layer, asking, is it possible to create a pure business logic layer that gathers all business logic unto itself?

55 comments:

  1. "This may not always be true, but I think it is true now."

    It will always be true - this is a fundamental aspect of computer science. For a programming language to be Turing-complete (i.e. able to handle any calculating task), it has to allow for open-ended loops. Therefore, if your language is to allow for any calculating task, it must allow for open-ended loops. So, as long as there are new problems to solve, there will always be unparameterizable code.

    ReplyDelete
  2. pengobatan penyakit glaukoma secara alami tanpa operasi

    Obat Herbal Glaukoma

    ReplyDelete
  3. your post is very interesting
    thank you for sharing
    i really liked

    Obat Herbal TBC

    ReplyDelete
  4. Hi..its a useful information.
    A well-managed email database is by far the best way to boost sales without risking budget waste because it appeals to consumers who have already opted in to receive marketing information from you. Germany Mailing Lists

    ReplyDelete
  5. Great Post, I'm just create tutorial iinstall yii2 framework too, check this cara install yii2

    ReplyDelete
  6. I agree with the most of the points in this article and it’s great without any doubt. I think you made some good points in Features also. Really an amazing post! I would like to suggest your blog in my friend’s circle. Here I find everything in details. I hope I will see this type of post again in your blog.
    Engineering Colleges, ECE Engineering Colleges in Chennai

    ReplyDelete
  7. Are you tired of being human, having talented brain turning to a vampire in a good posture in ten minutes, Do you want to have power and influence over others, To be charming and desirable, To have wealth, health, without delaying in a good human posture and becoming an immortal? If yes, these your chance. It's a world of vampire where life get easier,We have made so many persons vampires and have turned them rich, You will assured long life and prosperity, You shall be made to be very sensitive to mental alertness, Stronger and also very fast, You will not be restricted to walking at night only even at the very middle of broad day light you will be made to walk, This is an opportunity to have the human vampire virus to perform in a good posture. If you are interested contact us on Vampirelord7878@gmail.com

    ReplyDelete
  8. Influx Infotech have Team of Experienced Software & Website Developers. That works for you so that you can easily run your business.
    Website And Software Development Company
    Best School Management Software ERP

    ReplyDelete
  9. Software development company in Noida team has to perform in accord with the client's engineering team for ideal results. Techsaga Corporations address this facet of the software development process most aggressively with our streamlined and well-planned resources.

    ReplyDelete
  10. I saw your site to be epic for my necessities. It has sensible and strong posts. I've killed by a wide edge an epic piece of them and took in a ton from them. You're achieving some astonishing work. Grateful to you fit for making a titanic confusing site. If you have some problem with Hp Printer Offline click the link.

    ReplyDelete
  11. Today, Emblix as one of the best and top most service-oriented Digital Marketing Agency in Hyderabad and India , Which provides high-quality result-oriented Digital Services ranging from SEO to Web Design, Social Media Marketing and more, to a broad spectrum of clients from diverse industry segments. Through a well-oiled combination of Quality Solutions, Transparent Pricing, helping brands connect with customers, Flexible Delivery & Contract Model with a firm commitment to deliver on time and to budget, Emblix has successfully built a strong relationship with clients based on mutual trust and respect. Further, Emblix’s extensive market experience and expertise in Digital Marketing helps clients in successfully managing data as a strategic asset.

    ReplyDelete
  12. hi dude

    thanks for the posting and very unique article . keep posting more useful topics and ideas

    ReplyDelete
  13. Some popular Gps devices like- Magellan devices like magellan maestro 3200,magellan roadmate 1440,magellan maestro 4040, magellan roadmate 5235t-lm,magellan roadmate 1700,magellan roadmate 1470, and other old and newer models. We’ll help you to get easy maps, software / Firmware updates.

    Are you planning a trip or going out and wondering if you could have the latest maps to make your trip or your work. Well, you’re a step away from the latest map updates. Latest map updates make sure that you always reach your destination on time with full-proof accuracy.

    ReplyDelete
  14. rpa uipath training in bangalore Are you looking for a training institute that offers the best prakalpana training in Bangalore? Look no further than Prakalpana Academy! Prakalpana Academy is the leading provider of prakalpana training in Bangalore.

    We offer aem training in bangalore comprehensive training program that covers all aspects of prakalpana, from basic concepts to advanced techniques. Our program is designed to help you learn the skills and knowledge necessary to become a successful prakalpana practitioner.

    pcb design training in bangalore Prakalpana Academy is located in the heart of Bangalore, close to all major transportation hubs.
    We are open

    ReplyDelete
  15. rpa uipath training in bangalore Are you looking for a training institute that offers the best prakalpana training in Bangalore? Look no further than Prakalpana Academy! Prakalpana Academy is the leading provider of prakalpana training in Bangalore.

    We offer aem training in bangalore comprehensive training program that covers all aspects of prakalpana, from basic concepts to advanced techniques. Our program is designed to help you learn the skills and knowledge necessary to become a successful prakalpana practitioner.

    pcb design training in bangalore Prakalpana Academy is located in the heart of Bangalore, close to all major transportation hubs.
    We are open

    Docker training Course in Bangalore Monday through Friday 9:00am to 5:00pm. Contact us today to schedule a consultation or register for our next class!

    ReplyDelete

  16. I must say you have done a very good job with this. In addition, the blog loads very fast for me on Internet explorer. Excellent Blog! Thank you so much for sharing. post utme form for elechi amadi polytechnic

    ReplyDelete
  17. Thank you for the wonderful business submit. It's nice to come across a blog from time to time that doesn't contain the same outdated and mashed-up information. Thanks so much for sharing. fuwukari cut off mark for information technology

    ReplyDelete
  18. สล็อต เว็บ ตรง จากค่ายเกมสล็อตออนไลน์ชื่อดังอันดับ 1 ที่นักพนันทั่วโลกให้การยอมรับว่าเป็นค่ายเกมสล็อตออนไลน์ที่ดีที่สุดตลอดกาลกับค่าย PG SLOT ที่มาแรงที่สุดในปี 2022

    ReplyDelete
  19. A theme like yours with a few simple tweeks would really make my blog shine.

    ReplyDelete
  20. I just like the valuable information you supply in your articles.

    ReplyDelete
  21. I like this web blog its a master peace !

    ReplyDelete
  22. Glad I found this on google . You are amazing! Thank you!

    ReplyDelete
  23. Good article with great ideas! Thank you for this important article.

    ReplyDelete
  24. Fortunate me I discovered your website accidentally, and I am surprised why this didn’t happened in advance! I bookmarked it.

    ReplyDelete
  25. You’re really a good webmaster. The site loading speed is incredible.

    ReplyDelete
  26. Thanks for sharing wonderful blog !

    https://darshanelderscare.com/

    ReplyDelete
  27. I really like looking through a post that can make people think.

    ReplyDelete
  28. Also, thank you for allowing for me to comment!

    ReplyDelete
  29. Everything is very open with a precise explanation of the issues.

    ReplyDelete
  30. It was really informative. Your website is useful. Thank
    you for sharing!

    ReplyDelete
  31. Woah! I'm really loving the template/theme of this site.
    It's simple, yet effective.

    ReplyDelete
  32. A lot of times it's very difficult to get that
    "perfect balance" between superb usability and visual appeal.

    ReplyDelete
  33. Thanks for sharing such a wonderful post.

    ReplyDelete
  34. Excellent and nice post. It will beneficial for everyone.

    ReplyDelete

  35. I must thank you for the efforts you’ve put in writing this blog.

    ReplyDelete