Skip to Content

Anything wrong with the below statement

   CONSTANTS lc_5000 type werks_d value ‘5000’.

The compiler doesn’t complain – syntax checks are okay.

The code inspector doesn’t complain – as the following statements will use lc_5000 instead of ‘5000’.

Then what is wrong?  Because the whole reason on why 5000 needs to be  made a constant is lost.

The constant’s main purpose is readability and maintainability – neither of them are supported by a declaration like this.It is better off to hard code than to use a constant like this.

Readability – the person reading the code should understand what 5000 is , e.g. its plant in Atlanta. lc_5000 doesn’t say that.

Maintainability – Hypothetical situation, lets say before go-live , for some reason it was decided to rename  plant Atlanta to 9000.

Then what should we do, a short cut like    CONSTANTS lc_5000 type werks_d value ‘9000’. – most weird thing.

Or rename lc_5000 to lc_9000 and change all occurrences of lc_50000 – any advantage over hard coding?

The most ideal statement would be:

CONSTANTS lc_atlanta type werks_d value ‘5000’. This statement achieves both Readability and Maintainability.


To report this post you need to login first.

20 Comments

You must be Logged on to comment or reply to a post.

  1. Swanand Lakka

    Hello,

    Nothing is wrong with the statement. It is the use or requirement of the program that will decide if you need lc_5000 or lc_atlanta. That depends on ABAPer style and his need to use it.

    This is the definition of a constant.

    They allow you to store data under a particular name within the memory area of a program. The value of a constant must be defined when you declare it. It cannot subsequently be changed.

    So you put or declare constants where that value will not be changed in the entire course of the program.

    best regards,

    swanand

    (0) 
    1. Ram B Post author

      Swanand, thanks for your comment . You are right about definition of whats a constant?

      But can you please tell me what is the advantage of using a constant than using a literal in this case?

      (0) 
      1. Swanand Lakka

        hello,

        So in our case the value ‘5000’ is a literal. In you code you could use a statement like this

        If wa_itab-werks = ‘5000’.

        * Do something

        else.

        * Message

        endif.

        But to make the code readable a better way is

        If wa_itab-werks = lc_atlanta.

        * Do something

        else.

        * Message

        endif.

        So here lc_atlanta is constant and ‘5000’ is literal.

        Literal definition –

        Literals are unnamed data objects that you create within the source code of a

        program. They are fully defined by their value. You cannot change the value of a

        literal.

        best regards,

        swanand

        (0) 
        1. Ram B Post author

          Swanand, in the example you have mentioned it made sense because you were using lc_atlanta but I was asking  would there be any advantage of using lc_5000 over a literal.

          Rudiger(where-used) and Steve’s(memory references) comments below have some good points over why lc_5000 is better than a literal. But wouldn’t lc_atlanta have much more benefits?

          (0) 
      2. Nitin Kacker

        Hi,

        I think it does not matter whether you create a constant with the name: lc_atlanta or lc_5000 as long as it serves the purpose properly.Naming convention to be followed is a matter of convention and ergomical.

        Thanks,

        Nitin

        (0) 
        1. Ram B Post author

          Nitin, that was exactly my point- it is following naming convention and passes the syntax check but is it even worth it? Its better go literal than lc_5000.

          (0) 
  2. Rüdiger Plantiko

    Yes, Ram, the question is: What does constant do for me? What advantages do I have when using a constant rather than a literal? I think there are two:

    • Readability – as in your example: lc_atlanta is better than ‘5000’. Or zmd_plant-atlanta (with the constant defined as a structure in a type pool zmd – for each werks one component).
    • Where-used list: It may be an advantage if I can find out all occurrences of a certain constant in the code.

    There are many cases where none of these advantages is really convincing. I even prefer ‘X’ over ABAP_TRUE : Every ABAP programmer knows that ‘X’ stands for true, so there is not really a gain in readability – and nobody with a clear mind will ever make a where-used list for ABAP_TRUE .

    Many programmers habitually think “Literals – bad! Constants – good’. They are like the sheep in Animal Farm, baaing “Foor legs good, two legs bad”.

    Use constants only if they promise to do something for you.

    (0) 
    1. Steve Coolbaugh

      I don’t know if I can explain this appropriately, and this is a question.

      If a literal string is used repeatedly through out the program, does only 1 “copy” of the literal string get stored in memory when the program is loaded? Then other usages of the literal string point back to this one memory allocation?

      Or does each literal string instance get it’s own memory allocation?

      I know with a constant, the value is stored in one memory allocation, and using the constant just references back to that allocation.

      How does the abap runtime handle that?

      (0) 
      1. Ram B Post author

        Steve, you made  a good point – definitely lc_5000 has an advantage over ‘5000’. in the way the memory is allocated.

        Might be this is too minimal a savings in the HANA age but it is still a better to use a constant.

        (0) 
        1. Steve Coolbaugh

          I was not sure if that is a real concern; I thought the compiler may recognize the same literal being used in multiple places and only make the program allocate it one time; if that is the case, then there is no benefit to constant vs. literal string (in that regards).

          Not sure how abap handles that.

          (0) 
          1. Rüdiger Plantiko

            Steve, there seems to be a kind of reuse logic for literals implemented in ABAP. I made a little test report with four MOVEs:

                lv_x1 = gc_test.     ” Global constant in program
               lv_x2 = ‘abcdfghijk’.
               lv_x3 = abap_true.  ” Type pool constant
               lv_x4 = ‘abcdfghijk’.

            When stopping in the debugger and switching in the context menu to “ABAP Bytecode”, these statements are compiled to

            mvqb 01  0180  0000  0056
            mlqk 0A  0080  0001  0091
            mvqb 01  0180  000B  0057
            mlqk 0A  0080  000C 0091

            Observe that the two statements that seem to be the pendants of the literal moves have the same second argument 0091, which must be a lookup address.

            Indeed, when chosing the tool “System Areas” in the debugger and looking at area LITL (literals), I find the line

            90: 54616263 64666768 696A6B45 4E442D4F | TabcdfghijkEND-O

            containing the literal I entered, with offset 91.

            So it seems that the compiler is clever enough to store equal literals only once. There is an optimisation active for this. Of course, this one example doesn’t prove that it is always the case (which I don’t know).

            So constants and literals consume a similar amount of memory, they only access to different areas: Literals perform a lookup in LITL, constants loook up in CONST.

            So the compiler does not give us an argument for preferring literals or constants.

            Regards,

            Rüdiger


            (0) 
            1. Esti van Wyk

              Thanks for doing this test and sharing your results!

              This has been a topic of conversation between me and a colleague of mine several times.  She maintains that the use of constants is more efficient even if the potential gains are minuscule.  I’ve always wondered if the compiler would not be optimized for literals that are used more than once, but I still would like to find some documentation that confirms or deny this.  At least you’ve supplied us with some empirical evidence 🙂 .

              There is another benefit to use something like lc_500 in stead of the literal ‘500’ and that is if you use 500 repeatedly throughout your code, the syntax check will pick up a typo, whereas a literal is a literal and if you get it wrong the error will only be apparent at run-time.  

              Unfortunately I’ve seen several examples where the value of the constant needed to change and in stead of doing a search and replace for example lc_1 to lc_2, the programmer would just change the value of lc_1 to 2, which leads to really unmaintainable code.

              (0) 
              1. Rüdiger Plantiko

                Esti, thank you for your feedback! As I said – only a developer of the ABAP compiler could tell us whether the reuse of literals is a general strategy, but my example is a strong evidence for it.

                You are right – safety against mistyping at compile-time is a third advantage of constants beneath readability and where-used list, I had forgotten to mention – although important.

                But still Ram is right, lc_1 is a poor name for a constant.

                Of course, if a constant has to bechanged, it is tempting to change only its value in the declaration, leaving the name as it was: This would be a seemingly “elegant” one-line correction. But working this way means to spit on readability.

                In general, readability of code isn’t enough in the focus yet. It’s not sufficient  to have the code being executed properly by the machine. The code always is a design document – the only really true and essential design document – which is needed for subsequent developments. For code, readability by the machine and by humans are equally important.

                Regards,

                Rüdiger

                (0) 
    2. Ram B Post author

      Rüdiger, you are right , Where Used list is the big advantage of using a constant over a literal.

      And true ‘X’ is more convenient and makes sense  over ABAP_TRUE , only sometimes this might accidentally cause problems when using ‘x’ instead of ‘X’.

      Many programmers habitually think “Literals – bad! Constants – good’.  –  I cant agree for more. I have seen people at extremes with this rule –  sy-subrc results are made constants like lc_4, lc_8 🙂

      (0) 
      1. Ram B Post author

        And I found an other extreme/bizzare method – people choosing text elements instead of constants. Their argument its neither a constant nor a literal –  I just thought I will not fight this fight 🙂

        (0) 
  3. Steve Coolbaugh

    You know, I believe the practice of using constants like lc_5000 in your case above may be due to constant usage like lc_x; since in that case it really does just mean X.

    In summary, you are arguing the constant should be named after what the data means, not what the data value is; which I agree with whole heartedly.

    (0) 

Leave a Reply