tags:

views:

214

answers:

4

I occasionally do something like....

IF very-likely-condition THEN
    NULL;
ELSE
    <<code to deal with the unlikely condition>>
END IF;

Which gives a PLW-06002 unreachable code warning from the PL/SQL compiler on the NULL line atfer the IF.

Now whilst I can clearly ignore the warning and/or refactor the IF statement to be a NOT, I think it reads better this way.

So does anybody know is there is another way of inserting an empty statement so that I don't get the compiler warning?

EDIT:

I'm not saying I do this often... in fact I'd do it very rarely. But occasionally I do think it reads better this way.

EDIT 2:

Plus there are other scenarios where it might be valid to do this (such as ignoring a specific error in an EXCEPTION block). I only used the IF as a simple example to illustrate the point.

A: 

Why do you have an empty statement? That's a code smell. It's generally accepted that is it not easier to read with an empty if block.

Change your if condition to the opposite of what it currently is:

IF NOT very-likely-condition THEN
    <<code to deal with the unlikely condition>>
END IF;

If you need to do something when the condition is true, you can always add that block back in. Empty blocks separate the condition from the block that's executed when the condition is true. It also looks like you used to have code in the if section, then removed it but were too lazy to rewrite the if condition to remove the empty statement.

Subjectively, if I were reading your code and saw the empty if block, I'd think you didn't know what you were doing.

Welbog
I already said in the question that I knew I could do a NOT. I'm not saying that this is something I do often at all.... but in certain rare cases I do believe this is more readable. Do you have an answer to the question?
cagcowboy
A: 

I also prefer the "NOT very-likely-condition", but that's beside the point, since you've still got valid PL/SQL.

Re: your actual question, it sounds like PL/SQL has determined that very-likely-condition is always true, and that the ELSE block is therefore unreachable.

Jim Ferrans
Actually, I believe the error occurs for any use of the NULL; statement. The IF was just an example (which I'm beginning to regret using!)
cagcowboy
+3  A: 

To Recursive And Weblog :

the following statements are NOT equivalent:

IF :x = 0 THEN
   NULL;
ELSE
   do_something;
END IF;

and

IF NOT :x = 0 THEN
   do_something;
END IF;

If :x IS NULL the do_something procedure will be called in the first case only. This is because the expression NULL = 0 is neither TRUE nor FALSE in Oracle, it is "unknown".

The correct way to re-write the first statement would be:

IF :x != 0 OR :x IS NULL THEN
   do_something;
END IF;

I can see why in some cases we could write things as the OP.

Vincent Malgrat
+2  A: 

Looks like this is by design. See http://download.oracle.com/docs/cd/B19306%5F01/appdev.102/b14261/controlstructures.htm#i5421

Example 4-23 Using NULL as a Placeholder When Creating a Subprogram

CREATE PROCEDURE ... AS
BEGIN  
  NULL; -- use NULL as placeholder, raises "unreachable code" if warnings enabled
END;
/
jva