Technical Articles
method name scanner and statistics
Dear community, what are the “ingredients” of this blog? We have a blog about software metric by Christian Drumm, a blog about working with strings by Frederik Hudak, my blog with some thoughts on finding class and method names and Uncle Bob’s recommendation “You should name a variable using the same care with which you name a first-born child.” (thanks to Enno Wulff for the quote) … a few more considerations from me … mix everything … wait 5 minutes … et voila, we have a “method name scanner” and are able to generate basic statistics about method names 🙂
What’s the story? I’ve noticed that it takes a lot of my time to find good methods names during my work. In the sense of “Clean ABAP” I want to do it as neatly as possible because the method names should already document my source code.
However, 30 characters are sometimes not enough. This can be an indication that I want to achieve too much with a method or 30 characters are really not enough due to the terms I choose. In any case, it takes me time to find the right method name.
After thinking about it for some time, I didn’t find a “simple” solution to my problem. Experience probably speeds up the process of finding the right method name 🙂 Therefore I had the idea of learning from the work of other ABAP developers. But how?
Idea
A SAP ERP system has a large repository of development objects. This also includes the classes with their methods. The method names can be found in the SEOCOMPO database table.
Since spaces in methods are usually marked with an underscore, the idea was to analyze the method names for their words and the position of the words. So you can generate simple statistics which words have been used in which places and how often.
Use
Here are a few ideas for meaningful use:
- In connection with a code review, the methods of the classes which have been created in a project can be checked for noticeable words in advance.
- Based on the choice of words, you can conclude whether words from the solution or problem domain were used.
- Abbreviations used in method names can be checked for clarity.
- You can improve your own vocabulary for the design of method names.
Implementation
I wrote a “Proof of Concept” report to check my idea. The report can be imported from this GitHub repository via abapGit. At the moment my report doesn’t have an ALV output.
For analysis, you therefore have to set a breakpoint at the end of the “ANALYZE_METHOD_NAMES” method and look at the STATISTICS table while debugging. The table can be downloaded via debugger as a file for further processing in Excel or use the ALV output via debugger.
The structure of STATISTICS is as follows:
Column | Description |
WORD | A word used in a method name. |
COUNT_USED_OVERALL | The number of times a word was used in method names. |
COUNT_USED_AT_THE_BEGINNING | The number of times a word was used at the beginning of a method name. |
COUNT_USED_IN_THE_MIDDLE | The number of times a word was used in the middle of a method name. |
COUNT_USED_AT_THE_END | The number of times a word was used at the end of a method name. |
REFERENCES | This is a link to a table where you can check in which method names the word was used. |
Example
I used my demo report to create statistics of classes with prefix “CL_GUI_” on my SAP Netweaver 7.52 stack. Because there are many classes with even more methods, the view is a bit like “looking at the stars”. Everything at once doesn’t work because of the long run time.
It’s therefore best to narrow your view to certain areas (selecting the classes by application component would certainly be a useful improvement ;-)).
Here is an overview of the results:
- 1,408 different words or numbers were used to build method names.
- The different words were used 10,729 times.
- Space was found due to method names like “_DELETE_ALL” or “__CALC_RGB”.
- 876 words were used more than once.
- 678 words were used only once.
- The majority of the terms are from the technical area (like “UI” or “GRID”). That fits the expectation based on the class selection.
Here are some details:
Word | Count overall | Comment |
SET | 837 | Ok, no surprise. |
GET | 673 | I wouldn’t name my first-born child SET or GET 😉 |
DATA | 118 | Hopefully it’s clear what data is meant. |
MATRIX | 6 | No methods like “TAKE_RED_PILL” or “TAKE_BLUE_PILL” but “SET_MATRIX_VALUES”. |
HAIRS | 4 | Used in method name in combination “GET_CROSS_HAIRS”. |
CHECKMAN | 2 | Used in method name “UC_CHECK_CHECKMAN”. |
4 | 2 | Used in method names like “TREE_BREAK_4_ANALYSE”. This saves space. |
2 | 2 | Used in method names like “SUBSCRIBE_2_DISPLAY_MESS”. This saves space. |
Further ideas
- The scan could be extended to class and attribute names.
- Unfortunately I didn’t find any AI in SAP Leonardo to check if a word is noun or verb. But that seems to work with Google Spreadsheet.
- Improvement of the select options.
- <your ideas, please comment>
Best regards, thanks for reading and have fun
Michael
Puh, naming... a hard topic... 😉
I am not sure if a name metric can be helpful in order to have an indicator "roughly ok" or "please check your method names"?! Naming depends on so many things.
So let's have a look...
Objects
Maybe the name scanner can give a hint like: You often use <object> in your method names but it's already included in the class name.
Reason for hint: short names w/o duplicates are easier to read.
instead of
<action>_<object>
I often follow the principle <action>_<object> for naming
The name scann might give a hint like: You are often (> 4 times) using the object "XYZ" at the end of a method name. Try to group them by putting the object to the front.
Reason: If the object name is at the beginning of method name, it is obvious which methods belong together.
Maybe exactly the opposite: Do put the action at the beginning because then it is more obvious what the method does.
Abbreviations and similar names
When searching functions, it's often hard to find all used variations of a name. You will have to search for many different abbreviations and similar names:
The name scanner can give hints like:
Conclusion
After all I think that the name scanner might be a helpful and might give useful hints on naming.
Even if AI-powered I do not think that it will categorize the naming in "good" or "bad", but maybe I am wrong?
Thanks for Sharing, Michael!
Hi Enno, thanks for sharing your experiences and thoughts. Your comment is imho the perfect continuation of my blog content 🙂 Some thoughts from me (I hope there are more comments, I find this really exciting)...
Objects: I like the short form. The 30 character limit is often a tough nut for me. However, the instance of a class must then really have the appropriate name. DATA(bla) and DATA(blub) are really not ok 😉
<action>_<object>: I follow this principle, too. According to your example, the scanner should be adjustable.
Abbreviations and similar names: Very difficult topic. You would perhaps also have to consider the content of a method?
Conclusion: Good question if an AI-powered decision makes sense. I would like it if the Quickfix in the ADT could help you find meaningful method names. But I also have no idea how that could work in practice. It is really a difficult topic. In my view, only experience and discussions really help.
Categorising into good or bad, sounds like an ideal task for a neural net. It would be a matter though of assembling enough training data. Given SAP's huge code base, it shouldn't be too onerous - but whether you'd find enough "good" names ...?