When defining a variable containing a text of some sort in ABAP, which of the following options do you typically use?
" Option 1 DATA(my_text) = `my text`. " Option 2 DATA(my_text) = 'my text'.
Are you aware of the differences between these two lines of code? Let us look into the details.
What we are looking at are two different types of character literals. The ABAP Keyword Documentation provides us with the following explanation:
"Character literals can be either text field literals or text string literals. A text field literal is a character string enclosed in single quotation marks (
'); a text string literal is a character string enclosed in single backquotes (
Revisiting our example, we can define the variables like this:
" Option 1 DATA(my_text_string_literal) = `my text`. " Option 2 DATA(my_text_field_literal) = 'my text'.
my_text_string_literal has the actual type STRING and the technical type CString of length 7.
my_text_field_literal has a generated actual type (e.g. %_T00006S00000000O0000000298) and the technical type C of length 7.
The text field literal can have a length between 1 and 255 characters, whereas a text string literal can have a length between 0 and 255 characters.
That the text field literal has a minimum length of 1 means that
'' (no space) has the same meaning as
' ' (a space character).
When to use which character literal?
The SAP Styleguide for clean ABAP contains the following recommendation:
"Refrain from using
', as it adds a superfluous type conversion and confuses the reader whether he's dealing with a CHAR or STRING"
The following is an example of a superfluous type conversion between CHAR (from the text field literal) and a STRING:
DATA example_string TYPE string. example_string = 'QWERTY'.
Prefer the following assignment:
DATA example_string TYPE string. example_string = `QWERTY`.
If however the data element of the variable has been defined as a CHAR, the text field literal should be used to avoid a type conversion:
DATA e_mail_address TYPE ad_smtpadr. " CHAR 241 e_mail_address = 'email@example.com'.
I hope that this short post helped clarify the difference between
`. Happy coding!
If you would like to comment on this post, please head over to SAP Community.