Technical Articles
Interesting Facts on ABAP OO
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,
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.
I learned OO through learning Java and then applying what I learned to ABAP Objects. Well, I say "learned" - the fact is I'm still learning new approaches now, 15 years later!
I agree that using ADT instead of SE24 does change your approach. What SE24 has that, so far as I am aware, ADT doesn't, is the ability to use version management on a single method. What ADT has of course that SE24 doesn't is the ability to use version management on whole class sections!
ADT also has local versioning. Everytime you save, a new version is saved locally. That can be enormously useful when you go in the wrong direction and then an hour later need to backtrack.
ADT's unit testing is far better than SE24.
I have colleagues who are still stuck on SE24. As the oldest member of my team, I thought I was supposed to be one resistant to change! ?
SAP GUI tools were already working and world class. I never understood the rationale behind the ADT because it looks like bolted approach. May be SAp wants to provide a Java like development environment but ADT is still a work in progress and terribly lacks a lot of easiness and good features.
ADT is my preferred tool nowadays. I can develop and refactor far more quickly than in SAPGui, there's local version management (on every save), and ABAP Unit Tests are so much nicer there.
It is only a work in progress if you consider Eclipse to be a work in progress. Nowadays it's called "continuous improvement".
Anyway, when you're working with the cloud, you won't have sapgui.
(Oh, and if you think SAP GUI tools are world class, then I can only think you've not seen much of the world).