cleancode.in

Clean code in SAP ABAP

Clean ABAP is SAP’s adaptation of Clean Code to ABAP: names that say what things hold, snake_case, no Hungarian prefixes, small methods, exceptions over return codes, lines of 120 characters at most.

What does Clean ABAP ask for?

  • Use names that convey the content and meaning of things. source

From Clean ABAP. SAP’s guide says where it comes from in its first line source.

How should SAP ABAP code look?

  • Lines of 120 characters at most. source

What else does it ask?

  • No Hungarian notation, no prefixes. source
  • Objects over procedures. source
  • Functional calls over procedural statements. source
  • Methods under 20 statements, best 3 to 5. source
  • Exceptions over return codes. source
  • Comments say why, not what. source
  • Write for the reader. source

How are names written in SAP ABAP?

KindExampleRule
Typesshopping_cartsnake_case everywhere, as ABAP ignores case. source
Functionsadd_itemsnake_case everywhere, as ABAP ignores case. source
Variablesitem_countsnake_case everywhere, as ABAP ignores case. source
Constantsmax_itemssnake_case everywhere, as ABAP ignores case. source

What does cleaner SAP ABAP look like?

Before
DATA: lv_cnt TYPE i,
      lt_ord TYPE ty_orders.

CALL METHOD lo_db->read_orders
  EXPORTING
    iv_cust   = lv_cust
  RECEIVING
    rt_orders = lt_ord.

DESCRIBE TABLE lt_ord LINES lv_cnt.
After
DATA(orders) = order_db->read_orders( customer ).
DATA(order_count) = lines( orders ).

What changed?

  • The prefixes lv_, lt_, lo_ and iv_ are gone. source
  • Names that say what they hold: orders, order_count. source
  • A functional call and the lines( ) function in place of CALL METHOD and DESCRIBE TABLE. source
  • Inline declarations: each variable is declared where it gets its value.