code pal for ABAP > Documentation > Optional Parameters
This check searches for OPTIONAL parameter in method signatures. It is recommended to avoid the usage of OPTIONAL
parameters because they might confuse the consumers of the class:
- Which parameters are really required?
- Which combination of parameters are valid?
- Which parameters exclude each other?
Multiple methods with specific parameters for the use case avoid this confusion by giving clear guidance which parameter combinations are valid and expected.
Splitting methods (creating new ones) instead of adding OPTIONAL
parameters.
Before the check:
METHODS do_one_or_the_other
IMPORTING
what_i_need TYPE string OPTIONAL
something_else TYPE i OPTIONAL.
After the check:
METHODS do_one_thing IMPORTING what_i_need TYPE string.
METHODS do_another_thing IMPORTING something_else TYPE i.
In exceptional cases, you can suppress this finding by using the pseudo comment "#EC OPTL_PARAM
which should to be placed after the method declaration:
METHODS do_one_or_the_other
IMPORTING
what_i_need TYPE string OPTIONAL
something_else TYPE i OPTIONAL. "#EC OPTL_PARAM