Methods
Methods are the functions of ABAP classes, the units of behaviour that do the work. Writing well-designed methods with clear parameters is core to clean object-oriented ABAP.
Keep methods small and single-purpose, prefer RETURNING for a single result (enabling clean inline calls), name them for what they do, and handle errors via class-based exceptions (RAISING).
- A method is a named block of code inside a class that performs a task, optionally taking input and returning output.
- IMPORTING: inputs passed in.
- EXPORTING: outputs passed back.
- Watch out: Giant do-everything methods.
What a method is
A method is a named block of code inside a class that performs a task, optionally taking input and returning output. Methods encapsulate logic so it can be called, reused and tested. They replace the older FORM subroutines and are the building blocks of OO ABAP.
Parameter kinds
- IMPORTING: inputs passed in.
- EXPORTING: outputs passed back.
- CHANGING: in-out parameters.
- RETURNING: a single return value (enables functional-style calls).
- RAISING: exceptions the method may raise.
A functional method call
METHODS get_discount IMPORTING iv_amount TYPE p
RETURNING VALUE(rv_disc) TYPE p
RAISING cx_invalid_input.
" call it inline
DATA(lv_d) = lo_pricing->get_discount( iv_amount = 1000 ).Good method design
Keep methods small and single-purpose, prefer RETURNING for a single result (enabling clean inline calls), name them for what they do, and handle errors via class-based exceptions (RAISING). Small, well-named methods make code readable and testable.
Common pitfalls
- Giant do-everything methods.
- Overusing EXPORTING where RETURNING is cleaner.
- Ignoring exceptions instead of raising/handling them.