Monday, March 29, 2010

If 1 equals 2, what’s the purpose?

While debugging standard SAP ABAP code you might find for first look very strange code like following:
 

If 1 EQ 2.
MESSAGE W001(z100).
* plus do something else

EndIf.

Confusing? Of course! Based on the math this statement cannot be ever true. Then why do we need such an IF statement?


Actually there are certain situations when we need it. Just to name few of them. We need to make visible some parts of code which must not be executed under any circumstances. In this case we put it to such an IF. This can be make traceable usage of such a statement. E.g. message created in message’s transaction (TA SE91). In this way we just create reference point to “Where used” functionality of specific message. In case we would like to see where particular message 001 from message class Z100 is used we can achieve it. In general by this technique we can enhance ABAP code maintainability in larger scale.


This technique is often used in BW area as well. E.g. in data extraction ABAP function modules. See function modules called RSAX_BIW_GET_DATA_SIMPLE.

4 comments:

Anonymous said...

Nice tiny post. I have also come across the same piece of code in SAP IS-Ulilities packages. There, '1 = 2'-like statements are followed by message log maintenance (available by standard FUGR EMSG) often implemented as macros. Then it looks like this..

mac_msg_putx co_msg_warning '001' 'z_msg' space space space space est_gen_error.
IF 1 = 2. MESSAGE w001(z_msg). ENDIF.

which benefits from back-traceability and the fact that you can easily display the message 001 by double-clicking it, which is, obviously, not possible from hardly traceable macro..

reg,
V.

sapper said...

Hi V., thanks for stopping by at my blog and sharing your experience!

Anonymous said...

I read 2 other descriptions, but now I finally understood. well done! :)

sapper said...

Hi Anonymous, your feedback is very much appreciated!

sapper