Skip to content

Latest commit

 

History

History
175 lines (138 loc) · 8.84 KB

AlignParametersRule.md

File metadata and controls

175 lines (138 loc) · 8.84 KB

<-- previous rule | overview | next rule -->

Align parameters and components

Aligns parameter assignments in method calls, as well as component assignments in VALUE expressions, table expressions, table keys etc.

This rule is part of the essential profile, as it is explicitly demanded by the Clean ABAP Styleguide.

References

Options

  • Maximum line length A (normal) [120]
  • Maximum line length B (for tabular style) [160]
  • Procedural call: continue behind the call for up to [0] parameters
  • Functional call: continue behind the call for up to [100] parameters
  • Procedural call: put keywords (EXPORTING etc.) on own line
  • Functional call: put keywords (EXPORTING etc.) on own line
  • Align assignments
  • Align assignments across rows of table constructors
  • Table rows: Keep multiple components on single line [if maximum line length B is observed]
  • Keep other one-liners [never]
  • Allow line starts left of assignment operator [only to keep maximum line length]

Examples

  METHOD align_parameters.
    lts_table = cl_any_class=>create_table(
      EXPORTING
        iv_contract_id = lo_contract1->ms_data-contract_id
        iv_contract_type = if_any_interface=>co_any_contract_type
        iv_item_type = lo_item->get_item_data( )-item_type ).

    " commented-out parameters are aligned, too:
    lts_other_table = cl_other_class=>create_table(
      EXPORTING
        iv_item_key = '12345'
        iv_category = 'ABC'
*        iv_size = 100'
        iv_name = 'ANY_NAME'
        iv_qty = 8 ).

    CALL METHOD procedural_call_example
      EXPORTING
        iv_contract_id = lo_contract1->ms_data-contract_id
        iv_item_key = '13579'
      IMPORTING
        ev_category = lv_any_category
        ev_item_type = lv_any_item_type
      CHANGING
        cv_qty = lv_quantity.

    ets_table = VALUE #( date = gc_any_date
                           id = gc_any_id

                           ( item_name = 'ANY'
*                      size = 'M'
                              quantity = 1 )
                             ( item_name = 'OTHER'
                                quantity = 2
                                reference_id = '12345' )
                                   ( item_name = 'THIRD'
                                  quantity = 3 ) ).

    ets_fulfillment = VALUE #( event_date = gc_any_event_date
                                amount = gc_any_amount
                               ( fulfillment_number = lc_fulfill_num_1  qty = lc_fulfill_qty_1 )
                                 ( fulfillment_number = lc_fulfill_num_2  qty = lc_fulfill_qty_2 )
                                     ( fulfillment_number = lc_fulfill_num_3  qty = lc_fulfill_qty_3 ) ).

    " tabular style improves readability, therefore some overlength may be tolerated here (max. line length B):
    mts_table = VALUE #( ( item_key = '20220030000101'  event_date = '20220301'  total_qty = '30'  qty_unit = 'DAY'  amount = '1000.00'  currency = 'EUR' )
                         ( item_key = '20220040000101'  event_date = '20220401'  total_qty = '30'  qty_unit = 'DAY'  amount = '1500.00'  currency = 'EUR' )
                         ( item_key = '20220050000101'  event_date = '20220501'  total_qty = '30'  qty_unit = 'DAY'  amount = '2000.00'  currency = 'EUR' ) ).

    READ TABLE lt_any_table_name ASSIGNING <ls_table_row>
         WITH KEY field1 = ls_any_structure-field1
                  fld2 = ls_any_structure-fld2
                  long_field_name3 = ls_any_structure-long_field_name_3.

    result = VALUE #( BASE result ( id = 1 name = 'abc' ) ).

    LOOP AT lt_table ASSIGNING <fs>
         GROUP BY ( key1 = <fs>-any_component key2 = get_value( <fs>-other_component )
                     indx = GROUP INDEX count = GROUP SIZE )
         ASSIGNING FIELD-SYMBOL(<group>).

      cl_demo_output=>write( |{ <group>-indx } { <group>-key1 } { <group>-key2 } { <group>-count }| ).
    ENDLOOP.
  ENDMETHOD.

Resulting code:

  METHOD align_parameters.
    lts_table = cl_any_class=>create_table( EXPORTING iv_contract_id   = lo_contract1->ms_data-contract_id
                                                      iv_contract_type = if_any_interface=>co_any_contract_type
                                                      iv_item_type     = lo_item->get_item_data( )-item_type ).

    " commented-out parameters are aligned, too:
    lts_other_table = cl_other_class=>create_table( EXPORTING iv_item_key = '12345'
                                                              iv_category = 'ABC'
*                                                              iv_size     = 100'
                                                              iv_name     = 'ANY_NAME'
                                                              iv_qty      = 8 ).

    CALL METHOD procedural_call_example
      EXPORTING iv_contract_id = lo_contract1->ms_data-contract_id
                iv_item_key    = '13579'
      IMPORTING ev_category    = lv_any_category
                ev_item_type   = lv_any_item_type
      CHANGING  cv_qty         = lv_quantity.

    ets_table = VALUE #( date = gc_any_date
                         id   = gc_any_id

                         ( item_name    = 'ANY'
*                           size         = 'M'
                           quantity     = 1 )
                         ( item_name    = 'OTHER'
                           quantity     = 2
                           reference_id = '12345' )
                         ( item_name    = 'THIRD'
                           quantity     = 3 ) ).

    ets_fulfillment = VALUE #( event_date = gc_any_event_date
                               amount     = gc_any_amount
                               ( fulfillment_number = lc_fulfill_num_1  qty = lc_fulfill_qty_1 )
                               ( fulfillment_number = lc_fulfill_num_2  qty = lc_fulfill_qty_2 )
                               ( fulfillment_number = lc_fulfill_num_3  qty = lc_fulfill_qty_3 ) ).

    " tabular style improves readability, therefore some overlength may be tolerated here (max. line length B):
    mts_table = VALUE #(
        ( item_key = '20220030000101'  event_date = '20220301'  total_qty = '30'  qty_unit = 'DAY'  amount = '1000.00'  currency = 'EUR' )
        ( item_key = '20220040000101'  event_date = '20220401'  total_qty = '30'  qty_unit = 'DAY'  amount = '1500.00'  currency = 'EUR' )
        ( item_key = '20220050000101'  event_date = '20220501'  total_qty = '30'  qty_unit = 'DAY'  amount = '2000.00'  currency = 'EUR' ) ).

    READ TABLE lt_any_table_name ASSIGNING <ls_table_row>
         WITH KEY field1           = ls_any_structure-field1
                  fld2             = ls_any_structure-fld2
                  long_field_name3 = ls_any_structure-long_field_name_3.

    result = VALUE #( BASE result
                      ( id = 1 name = 'abc' ) ).

    LOOP AT lt_table ASSIGNING <fs>
         GROUP BY ( key1  = <fs>-any_component
                    key2  = get_value( <fs>-other_component )
                    indx  = GROUP INDEX
                    count = GROUP SIZE )
         ASSIGNING FIELD-SYMBOL(<group>).

      cl_demo_output=>write( |{ <group>-indx } { <group>-key1 } { <group>-key2 } { <group>-count }| ).
    ENDLOOP.
  ENDMETHOD.

Related code