Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

INITIAL vs. NOT BOUND

alejandro_bindi
Active Contributor

Could you explain me the difference? I understand that IS BOUND is to know if a reference holds an instance of an object. But using INITIAL appears to fulfill this requirement also, so there must be other difference I'm not seeing.

I've done this code for testing this and the output is

<b>INITIAL before CREATE OBJECT: TRUE

NOT BOUND before CREATE OBJECT: TRUE

INITIAL after CREATE OBJECT: FALSE

NOT BOUND after CREATE OBJECT: FALSE

INITIAL after CLEAR: TRUE

NOT BOUND after CLEAR: TRUE</b>


CLASS lcl_test DEFINITION.
  PUBLIC SECTION.
    DATA:
      atrib1 TYPE i.
    METHODS:
      constructor.
ENDCLASS.                    "lcl_test DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_test IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_test IMPLEMENTATION.

  METHOD constructor.
    atrib1 = 1.
  ENDMETHOD.                    "lcl_test

ENDCLASS.                    "lcl_test DEFINITION

CONSTANTS: c_true   TYPE string VALUE 'TRUE',
           c_false  TYPE string VALUE 'FALSE'.

DATA: o_test TYPE REF TO lcl_test,
      g_bool TYPE string.

START-OF-SELECTION.

  IF o_test IS INITIAL.
    g_bool = c_true.
  ELSE.
    g_bool = c_false.
  ENDIF.
  WRITE: /,'INITIAL before CREATE OBJECT:',g_bool.

  IF o_test IS NOT BOUND.
    g_bool = c_true.
  ELSE.
    g_bool = c_false.
  ENDIF.
  WRITE: /,'NOT BOUND before CREATE OBJECT:',g_bool.

*************************
  CREATE OBJECT o_test.
*************************

  IF o_test IS INITIAL.
    g_bool = c_true.
  ELSE.
    g_bool = c_false.
  ENDIF.
  WRITE: /,'INITIAL after CREATE OBJECT:',g_bool.

  IF o_test IS NOT BOUND.
    g_bool = c_true.
  ELSE.
    g_bool = c_false.
  ENDIF.
  WRITE: /,'NOT BOUND after CREATE OBJECT:',g_bool.

*************************
  CLEAR o_test.
*************************

  IF o_test IS INITIAL.
    g_bool = c_true.
  ELSE.
    g_bool = c_false.
  ENDIF.
  WRITE: /,'INITIAL after CLEAR:',g_bool.

  IF o_test IS NOT BOUND.
    g_bool = c_true.
  ELSE.
    g_bool = c_false.
  ENDIF.
  WRITE: /,'NOT BOUND after CLEAR:',g_bool.

Could you please explain the difference?

Thanks

Message was edited by:

Alejandro Bindi

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate

Well, the only thing that I would say is, that the IS BOUND syntax is specific to object references, so when you see this in your code, you automatically know that you are talking about an object, if you use IS INITIAL, it becomes ambiguous as to what you are talking about, since it could be a variable, internal table, or object reference. I believe that the IS BOUND syntax should always be used when referring to object references, this provides for better readibility and maintance.

Regards,

Rich Heilman

4 REPLIES 4

RichHeilman
Developer Advocate
Developer Advocate

Well, the only thing that I would say is, that the IS BOUND syntax is specific to object references, so when you see this in your code, you automatically know that you are talking about an object, if you use IS INITIAL, it becomes ambiguous as to what you are talking about, since it could be a variable, internal table, or object reference. I believe that the IS BOUND syntax should always be used when referring to object references, this provides for better readibility and maintance.

Regards,

Rich Heilman

0 Kudos

Ok, thanks Rich it's clear now.

0 Kudos

The <u>ref IS [NOT] BOUND</u> statement queries whether reference variable ref contains a valid reference. <i>ref</i> has to be a data reference variable or object reference variable here.

When a data reference is involved, this logical expression is true if it can be dereferenced; when an object reference is involved, it is true if it points to an object. The logical expression is always false if <i>ref</i> contains the null reference.

In comparison, the logical expression <u>ref IS [NOT] INITIAL</u> merely lets you determine whether or not <i>ref</i> contains the null reference.

A little bit philosophical

Actually this is not exactly the difference.

IS INITIAL - can be used with normal data-type and with reference-data-types

IS BOUND - is valid only for references. These are both object- and data-references.

(i.e. "TYPE REF TO object" or "TYPE REF TO data")

For object references, it doesn't matter, wether you use IS BOUND or IS INITIAL (IS BOUND is always preferable for references, tho)

The major difference is the handling of data-references. For data-references it is possible, that IS INITIAL is false and IS BOUND is false!!!

This happens when your reference points to local variable, which was automatically deleted at the end of a routine.

HTH,

Hristo