Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd coding style of IF block in PL/SQL

Tags:

sql

oracle

plsql

Where I work, I see lot's of the following type of code written in PL/SQL,

IF a>b THEN
  NULL;
ELSE
  c:=a*b;
END IF;

I find this odd because a C equivalent would look like this,

if (a>b)
{

}
else
{
  c=a*b;
}

And the above type of code has been frowned upon as bad style in a C forum that I know when posted by newbies. As PL/SQL doesn't allow empty blocks and always require a NULL statement, does this type of coding style bring any advantages w.r.t readability or is it just a matter of preference?. FWIW, the guy who coded PL/SQL with the above style certainly seems to be a seasoned coder. Is there any advantage compared to the following?

IF a<=b THEN
  c:=a*b;
END IF;
like image 640
toddlermenot Avatar asked Oct 22 '11 19:10

toddlermenot


People also ask

How to declare part of a block in PL/SQL?

In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. The approach is to divide the given number by 2 and if the remainder is 0 then the given number is even else odd.

How to count odd and even digits in a number in PL/SQL?

Count odd and even digits in a number in PL/SQL. In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a number and task is to find the number of odd and even digits present in the number.

How to make even or odd numbers in a block group?

A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. The approach is to divide the given number by 2 and if the remainder is 0 then the given number is even else odd. Below is the required implementation: Attention reader!

Why is it compulsory to specify the execution block in PL/SQL?

It is compulsory to specify the execution block. The PL/SQL executable block are a single unit that contains the business logic written inside it and which can also involve multiple data manipulation and retrieval of database values in its statements. These blocks can be given call multiple times if they are named blocks.


2 Answers

It seems that a <= b will not be true if any part is null. That's why these two statements are different.

In the first case, c = a*b will run if a is null. In the second case, it won't. Unless you know for certain that a and b are not null, the equivalent statement would instead be:

IF a<=b or (a is null) or (b is null) THEN
  c:=a*b;
END IF;
like image 91
Maep Avatar answered Nov 14 '22 10:11

Maep


Where I work, we do not like to have the empty blocks, so we always code ours like your bottom example, without the ELSE IF. I think it's largely a matter of preference though, but I would rather reverse the IF statement as you did at the end to avoid the empty block; I find the code easier to read.

like image 35
Adam Wenger Avatar answered Nov 14 '22 08:11

Adam Wenger