2014 Aug 18 9:01 PM
I have been reviewing the Official ABAP Programming Guidelines (SAP Press) and a point was made with regard to local classes within global classes. The point, if there is functionality/processing required within a global class that is only needed by that class it could make sense to create a local class within to handle this unique processing.
My question... Is there an advantage (or disadvantage for that matter) to creating an included local class instead of creating a private method for the same purpose? It would seem to require more effort on the front end of the class development to create an additional local class to handle some specific processing.
Justin
2014 Aug 18 10:01 PM
Is there an advantage (or disadvantage for that matter) to creating an included local class instead of creating a private method for the same purpose?
I can share my experience on defining local classes within global classes (excluding the ABAP unit classes).
We had to enhance a transaction to display a pop-up with data in tabular form & the user had to select one entry to continue processing.
Relevant BAdI was implemented and it was decided that the screen will be designed as an ALV popup. So in the BAdI implementation class i defined a local class & delegated all the ALV handling activities to it. My rationale behind this design was that the BAdI class is not responsible for handling the ALV & their "concerns" are different! Hence the ALV must be handled in a different class.
I could have created a global class for this, but i thought that BAdI & the ALV-popup are too tightly coupled. So i decided to use a local class within the BAdI implementation class.
BR,
Suhas
2014 Aug 18 10:01 PM
Is there an advantage (or disadvantage for that matter) to creating an included local class instead of creating a private method for the same purpose?
I can share my experience on defining local classes within global classes (excluding the ABAP unit classes).
We had to enhance a transaction to display a pop-up with data in tabular form & the user had to select one entry to continue processing.
Relevant BAdI was implemented and it was decided that the screen will be designed as an ALV popup. So in the BAdI implementation class i defined a local class & delegated all the ALV handling activities to it. My rationale behind this design was that the BAdI class is not responsible for handling the ALV & their "concerns" are different! Hence the ALV must be handled in a different class.
I could have created a global class for this, but i thought that BAdI & the ALV-popup are too tightly coupled. So i decided to use a local class within the BAdI implementation class.
BR,
Suhas
2014 Aug 19 1:24 AM
Hi Justin,
I liked Suhas answer for this (unfortunately unusual) good question.
My answer is that there is not a clear advantage per se; it will depend on your use case. Another use case is to create an local exception class, as I show:
But I believe most of the times private methods will do the trick.
Regards,
Custodio
2019 Apr 28 12:46 PM
Can we say : Private method of the class can be accessible making friend of the class but not local class methods can accessible outside ? So if requirement is strictly bound to particular class we can use local class in global class .
2014 Aug 19 6:18 AM
I use local classes when I need something doing that can be encapsulated, but tightly linked to the main class. If it isn't tightly linked, then I'll use a global class.
If you use a private method to achieve the same, without creating another class, then you are not encapsulating the complexity. This may or may not be the correct approach. It depends on what you're trying to do.
2014 Aug 19 7:31 AM
Hi,
I'd say that clean separation of concerns is always an advantage in itself, even if the "external" reuse of the locally implemented functionality is not (yet) intended. The main advantage lies IMO in the clarity and structure of the code and, even more importantly, the number of "roles" the global class itself plays/covers (even if only privately) is thereby reduced. It may be an illusory advantage, but it should also considerably ease re-factoring efforts should the external reuse become necessary.
cheers
Jānis
2014 Aug 19 1:43 PM
Thank you all. So to summarize...
Clearly there is no "one size fits all" answer.
2014 Oct 06 7:08 PM
Hello Justin,
I personally try to follow the recommedation that an object shall have a clear responsibility and try to avoid multi purpose classes. If a private method fits to the purpose of the class I create a private method. In case the new functionality does not fit well to class I prefer to move it to a different class. (There exists lot of proof that this is not the 100% case).
Classes with a single responsibility are easier to test which is a benefit for instance. On the other hand having lots of global classes make it hard to find out the key concepts in SE80 for me. Therefore I like to move helper classes with limited liklyhood for reuse in local classes. This makes life easier for me.
Best Regards
Klaus