Technical code vs. business code


Link to this posting

Postby Ursego » 19 Feb 2013, 22:17

Don't mix error processing (which is a purely technical code) with business logic.

"...and God divided the light from the darkness."

The First Book of Moses


The problem, described here, can happen if functions return success/failure code instead of throwing exceptions. So, if exceptions are utilized in your project then you can skip this topic.

Developers, reading the script, should concentrate only on the business issues. They don't have to investigate what is an error processing code (which is not interesting and should be automatically skipped) and what is the "interesting" "meat" of the program. In the following example, the method uf_get_orders_count() returns 0 (no orders found) or a positive number of found orders, and different business logic should be applied in these two cases; but the method can also return -1 which means failure to obtain the data (error) - the last is a very boring technical stuff which must be taken away from the business issues. See the next patterns and don't forget that the code fragments, appearing here as a couple of words in square brackets, really may be huge fragments of code!

*** BAD code: ***

Code: Select all
li_orders_count = this.uf_get_orders_count()
if li_orders_count = -1 then // error :-O
   [code fragment for banal error processing]
   return -1
elseif li_orders_count = 0 then // no orders found... :-(
   [code fragment for "No Orders Found" business scenario]
else // order(s) found!!!
   [code fragment for "Order(s) Found" business scenario]
end if

*** VERY BAD (!!!) code: ***

Code: Select all
li_orders_count = this.uf_get_orders_count()
if li_orders_count = -1 then // error :-O
   [code fragment for banal error processing]
   return -1
else
   if li_orders_count = 0 then // no orders found... :-(
      [code fragment for "No Orders Found" business scenario]
   else // order(s) found!!!
      [code fragment for "Order(s) Found" business scenario]
   end if
end if

*** GOOD code: ***

Code: Select all
li_orders_count = this.uf_get_orders_count()
// The next 'if' block is a standard technical code, so script readers will skip it automatically:
if li_orders_count = -1 then // error :-O
   [code fragment for banal error processing]
   return -1
end if

// Here begins the "interesting" business logic:
if li_orders_count = 0 then // no orders found... :-(
   [code fragment for "No Orders Found" business scenario]
else // order(s) found!!!
   [code fragment for "Order(s) Found" business scenario]
end if
User avatar
Ursego
Site Admin
 
Posts: 143
Joined: 19 Feb 2013, 20:33



Ketones are a more high-octane fuel for your brain than glucose. Become a biohacker and upgrade yourself to version 2.0!



cron
Traffic Counter

eXTReMe Tracker