Variables
Variables in ABAP hold the data your program works with. Understanding how to declare them, their scope, and modern inline declaration is basic but essential ABAP craft.
Follow naming conventions (e.
- Classic declaration uses DATA with a type.
- Variables declared in a program or method are local to it.
- Understanding how to declare them, their scope, and modern inline declaration is basic but essential ABAP craft.
- Watch out: Overusing global variables.
Declaring variables
Classic declaration uses DATA with a type. Constants use CONSTANTS. Variables can be typed from built-in types, dictionary types, or other data objects (LIKE). Modern ABAP supports inline declaration (DATA(...)) at the point of first use, which is cleaner and now preferred where it improves readability.
Classic vs inline declaration
" classic
DATA lv_total TYPE i.
lv_total = 10.
" inline (modern)
DATA(lv_total2) = 10. " type inferred as i
SELECT SINGLE * FROM mara INTO @DATA(ls_mara) WHERE matnr = @lv_matnr.Scope and lifetime
Variables declared in a program or method are local to it. Global data (declared at program top or in class attributes) lives for the program/object lifetime. Prefer local, narrowly-scoped variables, minimal shared state is easier to reason about and less error-prone.
Naming and clarity
Follow naming conventions (e.g. lv_ for local variable, ls_ for structure, lt_ for internal table) so code is readable at a glance. Clear names and tight scope make ABAP maintainable.
Common pitfalls
- Overusing global variables.
- Ignoring inline declarations that improve readability.
- Poor naming that obscures a variable’s type/role.