Skip to Content
-3

TYPES or DATA? Which one is better?

********* Using TYPES *************
TYPES: BEGIN OF lts_university,
       INCLUDE TYPE zcl_s20_universit_mpc=>ts_college.
       TYPES : departments TYPE ztts20_dept,
       * types: departments type zcl_s20_universit_mpc=>tt_department,
       * types: departments type table of zTS20_DEPT,
END OF lts_university.

DATA lt_university TYPE TABLE OF lts_university.
DATA ls_university TYPE lts_university.

********* Using DATA **************
DATA: BEGIN OF lss_university,
        INCLUDE TYPE zcl_s20_universit_mpc=>ts_college.
        DATA: departments TYPE zcl_s20_universit_mpc=>tt_department,
        * DATA: departments type table of zTS20_DEPT,
END OF lss_university.

DATA ltt_university LIKE TABLE OF lss_university.
***DATA lss_university LIKE ltss_university. 


My Observations :

1) In case of TYPES lts_university will have no memory allocated to it.
In case of DATA, we will already have a work area.
2) In case of TYPES, I am not able to use
a) departments type zcl_s20_universit_mpc=>tt_department
b) departments type table of zTS20_DEPT ( zTS20 is a database table )
In case of DATA, I am able to use both a) and b)

******************************************************************

Considering zcl_s20_universit_mpc=>tt_department and ztts20_dept, both to be table types with same structure fields, which one among TYPES or DATA one should use? Which one is better and why?

Regards,
Gopa

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Apr 12 at 08:43 AM

    Never use:

    DATA:BEGIN OF

    always use types. Why? You never know when you're going to pass data as a parameter. If you've defined the type, it's easy.

    You question is deficient. You've declared that:

    In case of TYPES, I am not able to use
    a) departments type zcl_s20_universit_mpc=>tt_department
    b) departments type table of zTS20_DEPT ( zTS20 is a database table )

    But you've not said why or what error message you get. It may simply be that you've not got the syntax correct. (I.e. problem lies between keyboard and chair, not with ABAP ;-) ).

    Add comment
    10|10000 characters needed characters exceeded

    • You shouldn't be using FORMs in the first place - they're obsolete.

      TYPE is not "for better performance.". You use LIKE, TYPE where appropriate to make the meaning of what you're doing as clear as possible.

      Sorry - I don't have the time for personal mentoring. I've already given you good hints. It's up to you to apply them.

  • Apr 12 at 08:28 AM
    Add comment
    10|10000 characters needed characters exceeded

  • Apr 16 at 09:11 AM

    you cant write "like"-statement in Abap-OO

    So time is saved when going with types.

    Types cant store Data. so when reading Code, you can read it faster when there are alot global types than when there are alot of global data-structures

    regards

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 13 at 05:32 AM
    -3

    It's fun to see answers like Never or Always do this because of a list of good reasons (even the rule given in the guidelines of the ABAP documentation), because this doesn't prevent an unlisted case to exist that makes that rule fail (as we say in French: "there is always an exception to a rule").

    For choosing between a "bound data type" or a "standalone data type", you should apply those 2 rules: YAGNI (you ain't gonna need it) and DRY (don't repeat yourself). I don't know the name of the principle of always selecting one way when there are two ways (kind of "reduce the knowledge required to the minimum"), which we use a lot in my consulting company because it's said that even non-developers should be able to understand and modify the code (maybe it's a branch of KISS principle).

    So, if you need to store data in a private structure used once, for instance for clearing all fields at a time, or define a private elementary variable of 2 characters for some reasons, I don't see the reason why you couldn't use a bound data type (YAGNI/KISS principles). Probably there would be better examples...

    If you have to use a public attribute, or a private data type used several times, then okay, define a standalone data type.

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 12 at 07:23 AM

    You can use table types in TYPES definitions.

    Check this part of ABAP documentation for table genericness:

    https://help.sap.com/http.svc/rc/abapdocu_751_index_htm/7.51/en-US/index.htm?file=abaptypes_keydef.htm

    Add comment
    10|10000 characters needed characters exceeded