Coding rules

Rule #7 Deal with error

Be error resistant.

Considering the following code

for bill_id in bill_id_list:
    count = some_sql(bill_id)
    result += count

You should use a try-catch construct to deal with errors. If one of the SQL request fails, you should still return a partial result.

I prefer the paradigm below

var counts = bill_id_list.map(function(bill_id) {
    return some_sql(bill_id);
var faulty_count = bill_id_list.length-counts.length; // number of unhandled items

I think of it in terms of :

  • Return a complete result
  • If some fail return a partial result, with at least the erroneous count
  • Else return a failure status, with useful details

In brief, don't let a single item break the logic for lack of compliance to your assumptions.