Wednesday, 29 July 2020 12:50

Interesting Facts on ABAP OO

Written by solen dogan
Rate this item
(0 votes)
“© 2020. SAP SE or an SAP affiliate company. All rights reserved.” “Used with permission of SAP SE”

ABAP OO INTERESTING FACTS

I remember when I first started learning Object orientation and its been quite a work, navigating through tcode: se24 is not exciting.

I wont mention writing an ABAP Unit test code, I always thought why do we need to do that. What you see is what you get!

The wizard was making so hard to generate test classes and i tried to avoid as much as then

That was totally wrong, I realized it with a slap on my face like any ABAPer.

When I first used ADT tools (Eclipse), I felt like I moved into a totally different world, excited.

We as ABAPers don’t see anything then SAP Gui, for goodness sake please change into something more exciting.

Its hard to get out of your comfort zone, it’s a painful one for sure.

Recently I started learning Java, it’s a big shift but an exciting one I should note.

Why?

Well there’s no end to learning.

Sometimes you need to cross the fence to see why the fence exists.

Have you seen the guideline from SAP in GitHub, there’s a huge section of ABAP clean code?

Thanks to all the guys put effort into this, i must say impressive and makes sense.

Please have a look,

https://github.com/SAP/styleguides/blob/master/clean-abap/CleanABAP.md#put-help-methods-in-help-classes

The interesting part is the comment on hungarian notation, at first i did not really agree on the uselessness of the hungarian notation but then i think with tools like Eclipse theres no point to use hungarian notation..

I must say it was so hard not to use it, how much habit went into this

Please try and see for yourself.

After watching the ABAP Freakshow by Thomas Jung, a great guy and recommend his show to you all.

There are many important topics and I am amazed how much it covers and goes into depth.

Especially the principles section is worth to look, I laugh at myself when I saw the comment

“Don’t start working on a backlog item by making a $TMP copy of a development object and playing around with it. Others won’t notice these objects and therefore won’t know the status of your work. You will probably waste a lot of time by making the working copy in the first place. You will also forget to delete the copy afterwards, spamming your system and dependencies. (Don’t believe this? Go to your development system and check your $TMP right now.)”

The bad solutions are named as Anti-Pattern, which teaches you the wrong way serving a higher purpose.

The section on writing ABAP unit tests especially great resource as the Test first or test-driven development takes the ABAP world by storm.

Here are some interesting facts on OO ABAP, in the form of question and answer.

Question: What’s the default way of passing parameters in methods in ABAP?

Answer: Theres two way of passing parameters in oo languages

Pass by value,

Pass by reference

However in ABAP all parameters are passed by ref to pass by value you need to explicitly mention it.

Examples:

METHODS: constructor IMPORTING value(author) TYPE string OPTIONAL

                                   reference(title)  TYPE string OPTIONAL

                                   !isbn   TYPE string.

Author is passed by value title and isbn is passed by reference.

! operator means by reference or you can leave it to default by reference.

Question: Is there method overloading of constructor overloading in ABAP like other OO languages?

Answer: Unfortunately there’s no concept of method overloading whereas you can try to use optional parameters for this case.

Lets say: in here only isbn is mandatory author is optional

METHODS: constructor IMPORTING author TYPE string OPTIONAL

                                   isbn   TYPE string.

Implementation:

METHOD constructor.

   if author is SUPPLIED.

    set_author( EXPORTING author = author ).

   endif.

  “ISBN is mandatory field  

   set_isbn( isbn = isbn ).

 ENDMETHOD.

Question: Can you create an abstract class without any abstract methods in ABAP?

Answer: The point of abstract classes is to have abstract methods where you can use it for inheritance as a superclass.

E.g   methods:  another_abstract_method abstract IMPORTING i_val TYPE i.

However in ABAP you don’t need to use any abstract method in an abstract class

SAP uses these abstract classes a lot throughout their framework

One god example is cl_abap_anit class which contains only Assertions but itself an Abstract class.

Question: We always talk about good programming practices such as Encapsulation in ABAP

How do you test private methods in Abap unit framework?

We always try to declare attributes as private to hide it.

Dont we

Also read https://rosettacode.org/wiki/Break_OO_privacy

But that brings up a big problem

How do we test these attributes or methods?

Answer: Take classes of package SABAP_DEMOS_CAR_RENTAL_APPL as an example, e.g. CL_DEMO_CR_RESERVATION_CATA. Check the includes where the test classes reside and where they are declared DEFERRED and as friends (local definitions and implementations).

İmagine that you are trying to test a class called zcl_book then you need to write

And the name of the local test class is lcl_testBook.

So declare the friends before the test then you will have access to all the private methods of zcl_book.

class lcl_testBook DEFINITION DEFERRED.

class zcl_book DEFINITION local friends lcl_testBook.

 

 

There will be more coming soon….

I hope you enjoyed reading these and find it useful.

 

 

 

 

 

 

 

 

 

Read 29 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.