Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Basic Question related to Negation IS INITIAL or IS NOT INITIAL

Former Member
0 Kudos

Hi Experts,

I am in confusion regarding very simple conditional statement.

which one is preferred in the following condition statements.

1. IF NOT so_vkorg-low IS INITIAL

Perform validation

ENDIF.

2. IF so_vkorg IS NOT INITIAL.

Perform validation

ENDIF.

3. IF so_vkorg-low IS INITIAL.

Do nothing

ELSE.

Perform validation

ENDIF.

I have read some where checking negation in a condition statement is not preferred, Instead go for other way around .

Expecting some valid points from experts

Thanks

Sreeni

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Sreeni,

I'm surprised to see such question coming up, but to be honest, I'm even more surprised to see the answers posted so far. Since release 6.10 the version [IS NOT INITIAL|http://help.sap.com/abapdocu_70/en/ABENNEWS-610-OTHERS.htm#!ABAP_MODIFICATION_3@3@] is available and should be used. The simple reason is that this is the most legible version and thus should be preferred over version 1. After all, ABAP programs are rather verbose and statements reflect English language. If in doubt read it to your grandma and check which version she understands - though I'm not saying that this should be the standard test for figuring out how to code....

Version 3. is obviously silly, because it's misleading (introduces a branch that is not required) and increases the number of statements (makes the program harder to read - at least in those cases where the additional statements are not inserted for simplifying some otherwise hard to understand construct/algorithm).

Anyhow, as mentioned by previous posters, there's no real performance difference among those statements. The statement you've heard about possible performance impact of negations refers most likely to database selects where a select with a condition containing a not equal (i.e. "<>") might result in the database ignoring an index for selection, though the selection via index would've been faster.

The layman's explanation to this phenomenon is that the cost base optimizer (i.e. the part of the database that decides how to read the data) sees a not equal, which indicates that for this field probably the majority of rows are matching (since you're only excluding one specific value). Such an assumption is based on an even distribution of data, which you don't always have. E.g. let's take an order status field with lots of completed historic orders in the system. If I select all the orders with status not equal to complete, I should end up with a fairly small number of orders (compared to the number of entries in the whole table) and thus should most likely use an index on order status if present (and no other better index is available).

Please note that my explanation of the possible performance issue is a simplified version and I've just added it to put your statement into context. In the end it means that once you have such a select, you should see whether it works fine or not and tune the statement if it's slow.

Cheers, harald

6 REPLIES 6

Former Member
0 Kudos

I prefer the 1st one..it worked for me...

In case 3...checks must b done 2 times...it will b better u stick with the case 1 as u need not have to perform two checks.Note that u have no actions to perform in first IF statement of case 3.

This is just a suggestion...

Former Member
0 Kudos

Hi Sreeni,

The general rule of thumb for IF-ELSE-ENDIF statements is that you must check which condition occurs most frequently, and then place that code in the IF block. This improves performance.

However, in this case, you don't want to do anything if your SELECT-OPTION is empty. Hence, follow the first syntax (option 1).

Regarding negation, you are right; it is always recommended not to use negative statements (this is also true in real life ).

Write your code as follows:


if not so_vkorg[] is initial. 
* write as 'so_vkorg[]' and not just 'so_vkorg' because this will select the header line only

Perform validation

endif.

Hope this helps! Do let me know if you need anything else!!

Cheers,

Shailesh.

vinod_vemuru2
Active Contributor
0 Kudos

Hi,

First two cases are same. No difference. You can avoid 3rd one as we are unnecessarily checking something and doing nothing. The one which you read regarding the negation is not in the conditional statements. It is in the WHERE clause of SELECT statement. Because negative conditions in WHERE clause bypasses the database optimizer.

PS: You can go with one of the first two.

Thanks,

Vinod.

Former Member
0 Kudos

Hi,

All three are equally correct ( in fact first two are quite same ), use any which you are comfortable with( Firs two are better to write ).

Regarding negative condition, this is avoided in select condition of where clause , but for if and else it dont cause any performance issue )

Former Member
0 Kudos

Hi sreeni,

Perfer to use first two are (both are same). Third one uses unnecessary if..else..endif condition, where no processing is done in the first alternatvie. If no processing is happening in one alternate condition inside the IF statement, Use only IF..Endif.

Regards

Vinod

Former Member
0 Kudos

Hi Sreeni,

I'm surprised to see such question coming up, but to be honest, I'm even more surprised to see the answers posted so far. Since release 6.10 the version [IS NOT INITIAL|http://help.sap.com/abapdocu_70/en/ABENNEWS-610-OTHERS.htm#!ABAP_MODIFICATION_3@3@] is available and should be used. The simple reason is that this is the most legible version and thus should be preferred over version 1. After all, ABAP programs are rather verbose and statements reflect English language. If in doubt read it to your grandma and check which version she understands - though I'm not saying that this should be the standard test for figuring out how to code....

Version 3. is obviously silly, because it's misleading (introduces a branch that is not required) and increases the number of statements (makes the program harder to read - at least in those cases where the additional statements are not inserted for simplifying some otherwise hard to understand construct/algorithm).

Anyhow, as mentioned by previous posters, there's no real performance difference among those statements. The statement you've heard about possible performance impact of negations refers most likely to database selects where a select with a condition containing a not equal (i.e. "<>") might result in the database ignoring an index for selection, though the selection via index would've been faster.

The layman's explanation to this phenomenon is that the cost base optimizer (i.e. the part of the database that decides how to read the data) sees a not equal, which indicates that for this field probably the majority of rows are matching (since you're only excluding one specific value). Such an assumption is based on an even distribution of data, which you don't always have. E.g. let's take an order status field with lots of completed historic orders in the system. If I select all the orders with status not equal to complete, I should end up with a fairly small number of orders (compared to the number of entries in the whole table) and thus should most likely use an index on order status if present (and no other better index is available).

Please note that my explanation of the possible performance issue is a simplified version and I've just added it to put your statement into context. In the end it means that once you have such a select, you should see whether it works fine or not and tune the statement if it's slow.

Cheers, harald