Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

What are constants?

Every programmer learns about constants in one of his first classes. Constants are used to refer to a single reserved block in the memory and can never be changed during runtime (at least in ABAP). It is defined once and can be referenced as many times as required. The main idea behind it is that if the value must be changed you only have to do this in a single place. Imagine a general TOP include which is used in many programs. Declaring your constants in here saves you a lot of effort when you need to change the value. This is nothing new.


Constantitis

People with constantitis have an uncontrollable urge to remove literals from their source code at all costs. They create constants blindly. Often this is accompanied by a total halt of logical thinking.


How do you recognize the symptoms? If you take a look at ABAPs you've written and you find constants named for example co_x (containing the value 'X') or co_1 (containing the value 1) then I'm sorry - my friend - you might have constantitis 😞


Luckily for you, it's not too late! Read on!


How constants should be used

An example: I create an ABAP for sales org NL01. Because the sales org value is used in the program I have to define a constant (because I'm a good programmer and I shiver at the sight of literals in ABAPs).


If I define the constant like this:

CONSTANTS: co_nl01 TYPE vkorg VALUE 'NL01'.


What happens when - in the future - I have to use my ABAP for salesorg DE01 instead of NL01? Because I have defined the constant in the top of my ABAP I know I only have to change the value once. What you get is this:

CONSTANTS: co_nl01 TYPE vkorg VALUE 'DE01'.


Although technically this works I hope we can all agree that this does not make sense. It's like saying 1 = 2. To make it more logical/readable you should also change the name of the constant and with that you screw up the entire concept of using constants.


So what I'm saying is: avoiding literals in your program by using constants is the right thing to do, but do not include the value in its name, like co_x with value 'X' or co_1 with value '1'. Choose a logical name (more like a description) such as:

CONSTANTS: co_salesorg TYPE vkorg VALUE 'NL01'.

or

CONSTANTS: abap_true TYPE abap_bool VALUE 'X'


This may sound logical and it should be. But I come across this a LOT, even in standard SAP code.

28 Comments