20

SAP doesn't have a core Data Type for boolean values. Additionally, higher level boolean types in SAP typically have three states: true ('X'), false (' ') and unknown ('-').

Now obviously booleans are a cornerstone of a lot of my development work but I realised that I haven't been terribly consistent in my type (data element) usage. So far I believe these are the most common:

  • abap_bool: defined in the abap type-pool, unconstrained but constants are defined for true, false, unknown, yes and no
  • os_boolean: data element, Yes ('X') or No (' ')
  • xfeld: data element, True ('X') or False (' '), lacks a field label, described as a checkbox

In my code I've mainly used abap_bool as I can then work with constants instead of character values, not that I couldn't assign abap_true to an xfeld. However, I've been cautioned that this type pool might not always be available.

I'm now wondering about the best practices for boolean values, specifically:

  • Is there a preferred type that I should use?
  • Will using the abap type-pool cause issues in certain modules or scenarios?
  • Does the possibility of abap_bool containing an unknown or indeed any character value matter?
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Lilienthal
  • 4,327
  • 13
  • 52
  • 88

4 Answers4

11

I use the type pool ABAP and its constants in coding. It should always be available, though you may have to include it manually on older systems. For dictionary elements, I prefer to create my own data elements using any of the default domains so that I can add descriptions to suit my needs. You can use WDY_BOOLEAN as well.

vwegert
  • 18,371
  • 3
  • 37
  • 55
  • The one place where this type can't be used is in Web Dynpro parameter definitions since the "Use of type pools is not supported in Web Dynpro". – Lilienthal Apr 24 '14 at 10:10
  • @Lilienthal - `WDY_BOOLEAN` can be used in WebDynpro applications. – mjturner Apr 27 '14 at 17:09
  • @mjturner `WDY_BOOLEAN` only supports binary values, not abap_undefined, but then so do `XFELD` and `OS_BOOLEAN`. – Lilienthal Apr 29 '14 at 08:31
  • @Lilienthal Indeed, that's true. Must confess, I'm not at all a fan of abap_undefined (and don't recall ever having to use it in anger!). – mjturner Apr 30 '14 at 21:53
  • @mjturner I definitely agree and can only recall a single time I used it as a tri-state return value. The assumption that booleans only have two possible states seems like a fairly safe one when developing in ABAP. – Lilienthal May 02 '14 at 12:56
  • @mjturner @Lilienthal The tri-state is VERY useful for optional parameters. There is no easy way to check optional parameters properly... `IS SUPPLIED` and `IS INITIAL` are both inadequate when you do recursive calling or call internal worker methods. – Marius Jan 13 '17 at 15:45
9

According to rule 6.11 of the Official ABAP Programming Guidelines, you should use abap_bool.

Rule 6.11: Use the abap_bool Data Type for Truth Values

To explicitly handle truth values, use the abap_bool type as a workaround for a real Boolean data type. A data object that is declared in this way is not supposed to contain other values than the corresponding constants, abap_true and abap_false (as well as abap_undefined).

Community
  • 1
  • 1
Eduardo Copat
  • 4,941
  • 5
  • 23
  • 40
  • you are also able to use WDY_BOOLEAN in Webdynpro Context, or BOOL_D in regular context as data element. You can also use FLAG, there are several ways, all doing the same ;) Even CHAR1 would solve the bool :D – dotchuZ Apr 23 '14 at 07:50
4

Update for Release 7.40, SP08:

With Release 7.40, SP08 you get the Predicative Method Calls and you can code something like:

IF cl_abap_demo_services=>is_production_system( ).  "There is no '= abap_true' needed!
    cl_demo_output=>display(
       'This demo cannot be executed in a production system' ).
    LEAVE PROGRAM.
ENDIF.

Your method (in the example is_production_system) must return a ABAP_BOOL-value (abap_true ('X') or abap_false (' '))

The online help has an example.

knut
  • 27,320
  • 6
  • 84
  • 112
  • Very interesting. But if I read [the docu](http://help.sap.com/abapdocu_740/en/index.htm?file=ABENpredicative_method_call_abexa.htm) correctly, any non-initial value for the returning parameter will be considered as evaluating to true. As you say, use of `abap_true` and `abap_false` will work for this though. – Lilienthal May 20 '15 at 18:07
-2

Sadly, this is the bane of ABAP... not having a fundamental boolean type... only boolean expressions. So the wonderful thing in ABAP is that there are so many boolean types to choose from!

After many years I believe the best way is to just roll your own (sadly). If you do class-based development, then always just add a true and false (and undefined if you wish) constants in your base class, and define your own boolean and/or boolean_undefined types.

Marius
  • 3,372
  • 1
  • 30
  • 36
  • 2
    Hmm, why would you define your own over using the `abap_...` constants provided in the ABAP type pool? I'm not sure the shorter and for new devs more obvious true/false weighs up to the extra maintenance effort and clutter. – Lilienthal Jan 13 '17 at 15:55
  • @Lilienthal , although type-pools will work, the problem is if you develop multi-release software. On certain older systems they are pretty sensitive as to where you include your type pools. Also, we prefer to just re-declare all our own types (even if a suitable one exists on the system) because it's more stable in case SAP decides to remove/change some in the future. Having your own type also means you can refactor easier when you alter your basic types. (Adding/altering `undefined` or `initial` as a bool type for instance?) – Marius Mar 03 '17 at 12:34