Application Development and Automation 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: 
Read only

How to Avoid Nested Loops in Module Pool Program?

Former Member
0 Likes
1,304

Hi all,

I am in the midst of developing a module pool program that uploads data into a z table.

The issue is that because of the many checks required (e.g. checking that the fields are not empty, checking which radiobutton has been selected), I have implemented several nested if else statements.

Essentially, the flow of logic is:

when p_manual is selected...
- if all the input fields have no value --> message user
- elseif all the input fields have value --> select data
---if selection successful, display data
---elseif selection not successful, ask user input data

when p_excel is selected...
- if the filepath field has no value --> message user
- elseif the filepath field has a value, then...
---if the user chooses to perform a test upload --> upload the data + display uploaded data back to user
---if the user chooses to upload the file --> upload the data + insert data into z table
<br>

Code is below:

*If user chooses to manually upload data
IF save_ok = 'X' and p_manual = 'X'. 
  IF field IS INITIAL. "check if field has no value
     "message user to fill in all fields
  ELSE. "if field has value
     "select data from zTable to display to user
    IF selected data is successful.
       "show the data in SM30
    ELSE. 
      "send user to another screen to manually fill in data
    ENDIF.

*if user chooses to upload data from Excel
ELSEIF save_ok = 'X' and p_excel = 'X'.
  IF filepath IS INITIAL.
     "message user to select a filepath
  ELSE.
     IF user wants to perform a test upload.
       "upload the data
        "call another screen to display the data
     ELSEIF user wants to upload the data directly into the z table.
        "upload the data
        "insert the data into table
     ENDIF.
  ENDIF.


ENDIF.

Does anyone have any suggestions on how to improve this (i.e. without using nested IF...ELSE statements)?

Thank you,
Yi Lin

4 REPLIES 4
Read only

FredericGirod
Active Contributor
1,217

Maybe you have to change totaly your mind and play with method & exceptions.

The part of your code is not enough to be sure it is the good way to do it, but it could be something like that

try.
  check is_field_populated( field... ) eq abap_true. 
  check is_selected_data_success( data ) eq abap_true.

  catch cx_application into data(lo_excption).
endtry.

for the part of the save data type, you have to split it. Try to folloy the Clean code and follow the rule : A method do one thing, and do it well

here, you do a lot of control, and it is a big melting of responsibility

Read only

matt
Active Contributor
1,217

"How to Avoid Nested Loops in Module Pool Program?"

I see no loop.

Read only

former_member24871
Discoverer
0 Likes
1,217

Hi, To validate user input fields use the below

FIELD Field_Name MODULE validate_field_name concept.

You can try with chain end chain if there is more than 1 field to be validated

Read only

Former Member
0 Likes
1,217

Hi Frederic, thanks for your suggestion. I will try to use that in my code.

Hi Matthew, thanks for pointing out the error in the title. That was a mistake on my part