24 March 2013

Defining "the same results" for "unnecessary code" quizzes

The discussions around choice 4 of the 19 March quiz, in particularly those titled "Choice 4's commit is unnecessary" and 'The "last_name" column is unnecessary in Choice 4', made me realize that I need to offer a more explicit definition of what it means for two blogs to have the "same result."

Some players interpreted this to mean "same output displayed on the screen." Others engaged in a very deep analysis of whether or not the commit of a row is the "same" as an uncommitted row in a single session, etc.

Just like all of you, the players, I do not want to have debates on these sorts of issues after quizzes. I would much rather discuss the Oracle technology behind the quizzes.

So I am going to come up with a definition of the "same result" for these quizzes.

And I will start by first offering a definition or way to prove that two blocks do not have the same result.

I run the same code (let's call it T for test code) after each block (let's call them A and B). If T displays "A" after running the A block, and displays "B" after running the B block, then A and B do not have the "same result."

Expressed algebraically:

If A = B, then A + C = B + C.

And, conversely:

If A + C != B + C, then A != B.

Let's apply this rule to the following block:

BEGIN
   INSERT INTO plch_employees
        VALUES (1, 'Splog', 1000000);

   INSERT INTO plch_employees
        VALUES (200, 'Rogash', 1000000);

   COMMIT;
END;
/

Several players argued that COMMIT is unnecessary. I claim that it is necessary. I will now prove it using the proposed rule.

I run the following block immediately after the above block (call it A) and another without the commit (call it B):

DECLARE
   l_count INTEGER; 
BEGIN
   ROLLBACK;
 
   SELECT COUNT(*) into l_count
     FROM plch_employees;
 
   IF l_count = 2 
   THEN 
      DBMS_OUTPUT.PUT_LINE ('A');
   ELSE 
      DBMS_OUTPUT.PUT_LINE ('B');
   END IF;
END;
/

I will not see the same output displayed on the screen. Those two blocks, then, do not have the same result. The COMMIT is, therefore, necessary code.

This rule gives us all a way to demonstrate that two blocks are not the same. So if anyone claims that a certain chunk of code is not necessary, that claim can now be dis-proven objectively - as long as a block of test code can be devised.

Note that the rules state:

A change in the resources needed to execute the choice (CPU, memory, etc.) does not, for this quiz, constitute a change in the choice. In other words, if the removal results in a choice that is slower or consumes more memory, but otherwise accomplishes the same work (inserts a row, displays text, etc.), then the choice does contain unnecessary code.

So test code cannot display "A" vs "B" based on a "change in the resources" as defined in this rule.

Well, I will start with this rule and see what you all think.