Never write such ABAP code

Source: Internet
Author: User

Today, I encountered a strange problem when I debugged the program for more than an hour and found that it was such an error. This problem is hard to be found, so I wrote it for your reference.

Do not write the ABAP program like this. Otherwise, you may not even know how to die:

 

I modified the program in the project and extracted the following paragraph:

 

Report z_test_loop.

Do 10 times.

Data: lv_bool type char1 value space.

 

If sy-Index = 5.

Lv_bool = 'x '.

Endif.

 

If lv_bool = 'x '.

Write 'true '.

Else.

Write 'false '.

Endif.

 

New-line.

 

Enddo.

 

The Code logic is very simple, that is, to make a loop, a total of 10 times, when the fifth time will be marked as true, it is reasonable to say that each cycle at the beginning will call data: lv_bool type char1 value space. therefore, if this parameter is not set, it is set to false.


The problem arises. Starting from 5th, all lv_bool values are true and cannot be changed again.

In the past, the variable scope of the ABAP program was a method or function module, rather than a block in our general concept. Therefore, when the system is in a loop, because the system variables have been defined, the defined field is no longer executed. The default value is the value obtained in the previous loop, which leads to the above problem. Therefore, the definition must be written as follows:

Data: lv_bool type char1.

Lv_bool = space.

If this program appears in a large program, it is still difficult to adjust. This article is only for your reference by abaper.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.