Watch out for redundant code with WHILE loops

Generally, you should use a simple loop if you always want the body of the loop to execute at least once. You use a WHILE loop if you want to check before executing the body the first time. Since the WHILE loop performs its check “up front,” the variables in the boundary expression must be initialized. The code to initialize is often the same code needed to move to the next iteration in the WHILE loop. This redundancy creates a challenge in both debugging and maintaining the code: how do you remember to look at and update both?

If you find yourself writing and running the same code before the WHILE loop and at end of the WHILE loop body, consider switching to a simple loop.

Here's an example.
I write a procedure to calculate overdue charges for books; the maximum fine to be charged is $10, and I will stop processing when there are no overdue books for a given date. Here is my first attempt at the procedure body:

DECLARE
   l_fine PLS_INTEGER := 0;
   l_date DATE := SYSDATE;
   l_overdue_count NUMBER;
BEGIN
   l_overdue_count :=
      overdue_pkg.countem (
         borrower_id => borrower_in,
         l_date);
  
   WHILE (l_overdue_count > 0 AND l_fine < 10)
   LOOP
      update_fine_info (l_date, l_one_day_fine);

      l_fine := l_fine + l_one_day_fine;
      l_date := l_date + 1;

      l_overdue_count :=
         overdue_pkg.countem (
            borrower_id => borrower_in,
            l_date);
   END LOOP;
END;   

As is readily apparent, I duplicate the assignments of values to l_overdue_count. I would be far better off rewriting this code as follows:

DECLARE
   l_fine PLS_INTEGER := 0;
   l_date DATE := SYSDATE;
   l_overdue_count NUMBER;
BEGIN
   LOOP
      EXIT WHEN
        (l_overdue_count <= 0 OR l_fine >= 10)
     
      update_fine_info (l_date, l_one_day_fine);
     
      l_fine := l_fine + l_one_day_fine;
     
      l_date := l_date + 1;

      l_overdue_count :=
         overdue_pkg.countem (
            borrower_id => borrower_in,
            l_date);
   END LOOP;
END;   


By paying close attention to your loop construction, you can avoid redundant code, always bad news in a program, since it increases maintenance costs and the chance of introducing bugs into your code.

Comments

  1. Great tip Steven!

    There is a subtle difference between the two versions though... In your second example, l_overdue_count = NULL in the first iteration. It requires that one knows how nulls are handled in calculations and boolean evaluations, e.g:

    (null <= 0) evaluates to null
    (null or a) evaluates to a

    But in the case of AND
    (null and false) = false
    (null and true) = false
    This last one can surprise you if you're not paying attention ;)

    ReplyDelete
  2. @Rop, thanks for writing, but I need some clarification. When I execute this block:

    DECLARE
    PROCEDURE bplstr (str IN VARCHAR2, val IN BOOLEAN)
    IS
    BEGIN
    DBMS_OUTPUT.put_line (
    str
    || ' - '
    || CASE val
    WHEN TRUE THEN 'TRUE'
    WHEN FALSE THEN 'FALSE'
    ELSE 'NULL'
    END);
    END bplstr;
    BEGIN
    bplstr ('(NULL AND FALSE)', (NULL AND FALSE));
    bplstr ('(NULL AND TRUE)', (NULL AND TRUE));
    bplstr ('(NULL OR FALSE)', (NULL OR FALSE));
    bplstr ('(NULL OR TRUE)', (NULL OR TRUE));
    END;
    /

    I see:

    (NULL AND FALSE) - FALSE
    (NULL AND TRUE) - NULL
    (NULL OR FALSE) - NULL
    (NULL OR TRUE) - TRUE

    What do you see?

    ReplyDelete
  3. *Sigh* of course you're right... my output is the same as yours. I fell into my own trap while trying to be smart.

    I only used an if/then/else, like this:

    if (null and true) then
    dbms_output.put_line('true');
    else
    dbms_output.put_line('false');
    end if;

    This outputs 'false' when the actual value is null.
    Well... it shows how difficult it is to evaluate null conditions ;)

    ReplyDelete

Post a Comment

Popular posts from this blog

Table Functions, Part 1: Introduction and Exploration

Recommendations for unit testing PL/SQL programs

The future of Oracle PL/SQL: some thoughts on Sten Vesterli's thoughts