Appearance
Lesson 2 Companion — Internal Tables, Loops & Methods
1. Internal table vs database table
An internal table is program memory. A database table is persistent storage. Students should not call internal-table operations database CRUD.
text
Internal table → temporary ABAP data
Database table → persistent data (introduced later)2. Why WITH EMPTY KEY
For simple lists where no table-key lookup is intended, an explicit empty key avoids accidental dependence on ABAP's standard-key rules.
3. VALUE vs APPEND
VALUE is excellent for construction. A loop with procedural updates is often easier to debug than a dense expression. Modern ABAP is not a competition to remove every statement.
4. sy-tabix
It is valid for index-table processing. It is not a stable business identifier and behaves differently with hashed keys.
5. Table expressions
abap
itab[ 2 ]is modern and concise. Missing lines can raise CX_SY_ITAB_LINE_NOT_FOUND; code should only assume success when the contract guarantees the line.
6. Functional methods
A method with exactly one RETURNING result can be used in expressions:
abap
DATA(total) = sum_numbers( numbers ).Named parameters remain preferable when several parameters could be confused.
7. Reference implementation
abap
CLASS zcl_abap_l02_### DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
TYPES tt_numbers TYPE STANDARD TABLE OF i WITH EMPTY KEY.
METHODS sum_numbers
IMPORTING numbers TYPE tt_numbers
RETURNING VALUE(total) TYPE i.
METHODS rectangle_area
IMPORTING width TYPE decfloat34 height TYPE decfloat34
RETURNING VALUE(area) TYPE decfloat34.
ENDCLASS.
CLASS zcl_abap_l02_### IMPLEMENTATION.
METHOD sum_numbers.
LOOP AT numbers INTO DATA(number).
total += number.
ENDLOOP.
ENDMETHOD.
METHOD rectangle_area.
area = width * height.
ENDMETHOD.
METHOD if_oo_adt_classrun~main.
DATA(numbers) = VALUE tt_numbers( ( 5 ) ( 10 ) ( 15 ) ( 20 ) ).
out->write( |Total: { sum_numbers( numbers ) }| ).
DATA(area) = rectangle_area(
width = CONV decfloat34( '5.5' )
height = CONV decfloat34( '4' )
).
out->write( |Area: { area }| ).
ENDMETHOD.
ENDCLASS.8. Empty-table design
For min/max methods, decide the contract for an empty table. Options later include returning an optional result structure or raising a class-based exception. Do not silently rely on line 1 if empty input is possible.