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;
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.
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.
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!
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.
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With