Tuesday 2 August 2022

OO. Ohhhhh. :(

OO stands for object oriented. It means that you write code around objects. For example:  An object could be a customer or supplier or a product or a delivery or a sales order or any other thing that has a unique identity. In Abap OO, when you create your own objects (called classes), their name has to start with the letter Y or Z. So a good name for a class could be ZCL_MATERIAL. A class name should contain a noun because it pertains to a thing (tangible or intangible) In SAP, the material is uniquely identified by means of a material number and this number should be kept in the attribute of that class. Any business logic that applies to materials can be kept inside the zcl_material class in logic blocks called methods. For example: You could have a method called get_stock_quantity. This method would return the number of pieces available of that material. A good method name contains one verb because it pertains to an action/operation/activity.

You can then do things like this

MyMaterial = new zcl_material( 12345 ).

If MyMaterial->get_stock_quantity( ) = 0

Message MyMaterial->get_name( ) && ‘ is out of stock’.

Endif.

This is reusable code consisting of reusable simple and single purpose building blocks. That is one of the key ingredients to high quality code. Code that is written in this style reads fairly easily and is understandable for testers and business consultants but also for the next programmer. This is extremely important to guarantee business continuity in a world where many programmers do short term assignments and then move on.

[WARNING: a rant follows]

If you are handed ABAP code that you cannot easily read, it is BAD code. Reject it! Do not put up with it. Tell the programmer to do a better job or if you are the next programmer, make it clear to your superiors that you have inherited BAD code and that enhancements to the code will take more time (times 3 is a good starting point) than one would typically expect and that you cannot give any guarantees on your work and that they will have to do extensive testing including regression testing. Do yourself and the rest of the SAP world a favour and speak up!


No comments:

Post a Comment