ABAP中的代码重复使用

news2025/2/27 5:32:04

文章目录

      • 1 Introduction
      • 2 Detail

1 Introduction

ABAP offers various options for avoiding such code duplicates and for giving a piece of program code a name under which it can be called:

Form routines (these are officially obsolete)
Function modules (this is what this is article is about) and
global classes and their methods. In most cases these are the better choice. A class can have multiple methods. This is a better structure than to have a large number of function modules that have little connection with each other.

2 Detail

Use ABAP Development Tools for Eclipse (ADT)
Is there an alternative to ADT? Nobody would use Notepad to write Java… Seriously: refactoring capabilities (rename method!) as well as ABAP Doc comments are essential features for us. Thus SE80 is not an option.

Put Everything in Classes and Methods
Do not place functional code in programs (reports) or function groups/modules. As we come from Java, this is quite obvious. We use reports and function modules only if there is no other solution possible, e.g. for providing an RFC interface. And also in the rare cases where we had to leave the OO-world, these classical ABAP only serve as a proxy delegating to the actual implementation in a class.

Structure the Source Code by OO Concepts only
Especially, do not use includes. In our own codebase, there was only one exception: When we migrated our code from Z to /CQSE/ namespace, we wanted to keep the old Z programs as legacy interface for a while. Thus the Z programs just included the new /CQSE/ programs, we preferd this approach over code duplication.

Rules for Naming and Usage of Identifiers
In ABAP, it is common to use type prefixes in identifier names (Hungarian notation), e.g. lr_foo for a local variable of a reference type. This is completely unusual in Java and really hard to read if you’re not used to.

But the major problem is that it is difficult to keep the prefixes consistent, as in details the correct prefix is not always obvious: Consider for example the following questions: Should a local variable referencing to an object be named lr_foo or lo_foo? Does ls_bar point to a string or a structure? Does iv_input mean that the input parameter is of a (primitive) value type or that it is passed by value? Since this is confusing, our conventions are:

No prefixes for variables. We don’t prefix variables, neither local nor member variables.
Always use me-> or some_class=> when accessing member variables. We use this to make the variable scope clear, even without prefixes. In Java we don’t do this, but for Java the IDE (Eclipse) formats the identifiers in different fonts depending on the scope.
Use prefixes for method parameters. ABAP knows—in contrast to Java—importing, exporting, changing and returning method parameter. Initially, we also used the parameters without any prefix, but this turned out to be not practical: as the method interface is defined separately from the method implementation, you typically don’t have the methods signature on your screen when reading a method’s implementation. Thus the prefixes i_, e_, c_, or r_ really help to instantly see, what a parameter is and if it is an importing, exporting, changing, or returning parametr. This is especially valuable for exporting and returning parameters to quickly locate what the method returns, thus instead for return you have to watch out for r_… to locate the return value.
Keep Method Calls Simple and Consistent
We strongly prefer to have methods with just input parameters and a returning parameter. We are using EXPORTING or CHANGE parameters with care. This helps to have a consistent and compact syntax for method calls like

在这里插入图片描述

instead of
在这里插入图片描述

We also avoid superfluous keywords such as in the above example EXPORTING or CALL METHOD (for static calls, the latter is obsolete syntax anyway).

Do not use CALL FUNCTION
… at least not in regular coding. As our own code is all in classes and methods we don’t have any own function module to call. But we also do not use CALL FUNCTION for SAP standard functionality as well—at least not in our regular coding. Of course, we also have to use SAP standard functionality which is actually implemented in a function module. But, instead of calling a function module directly, we wrap it in a class method of specific utility classes. For example, we wrap the call to function module GET_SYSTEM_TIMEZONE like
在这里插入图片描述

This has two advantages: First, in the regular coding we only have one consistent concept of modularization and a consistent way of calling procedures (see my point before). Here the call would be just time_zone = /cqse/cl_sap_util=>func_get_system_timezone( ).

Second, and more important, we only have to deal with class-based exception handling. The classical exceptions are also transformed in class-based exceptions, thus our wrapper methods throw /CQSE/CX_SAP_UTIL_EXCEPTION only. Sticking to a single concept of exception handling makes life much easier.

Do not call SAP Standard Code Directly
As said before, we wrap all function calls in specific proxy methods for it. Actually, this counts for any SAP standard functions we use, even if these are implemented in a method. Unfortunately, only very few of SAP’s API is formally released for public usage. For all the rest it is the developer’s risk to use it, since SAP may change it with any upgrade or patch level. The wrapper methods at least minimize this risk, as the fix to a changed behavior or API must be done in one place only. Luckily we’ve Teamscale’s architecture analysis available to easily identify calls to standard objects and make sure that these calls only occur in our /CQSE/CL_SAP_UTIL class.

Use ABAPDoc Consequently
Use ABAPDoc at all classes, interfaces and methods and type definitions. The same we did always with JavaDoc.

Don’t Copy
Of course, we follow the “don’t repeat yourself principle” consequently and strictly avoid duplicated code (code clones) within our code. Our current clone coverage (amount of duplicated code) is 0.6% - with just a single clone pair.

Finally: Keep Methods Short!
For Java, we try to keep our methods at a maximum of 30 SLOC (code lines without comment and blank lines). Even if we were aware the ABAP syntax is much more verbose then Java syntax, we aimed the 30 SLOC limit also for ABAP in the beginning. But meanwhile we have to admit that 30 SLOC is not really practical for ABAP, as it would lead to too many artificial method splits. Thus we changed the limit to 40 SLOC. And this works very well. We tolerate a few longer methods, but 89% of our code is within this 40 SLOC limit, our longest method has 61 SLOC. Strictly using the compact form of method call syntax and wrapping the lengthy function calls in methods makes this not as difficult. For further reasons behind this guideline on ABAP method length see my earlier post.

Summary
With all these guidelines, programming ABAP does actually not feel that different any more than programming Java. Still there are a few concepts where we follow traditional ABAP coding standards, e.g. we place all declarations at the beginning of a method and do not use inline declarations. We’d really love to use the latter, but we also must ensure that our code of the Teamscale Adapter runs on systems with ABAP < 7.40 as well. To summarize, it’s just a few rules one have to follow for a more modern ABAP programming style:

No superfluous prefixes
Only use the object oriented programming paradigm
Wrap function calls to unify exception handling
Use ABAPDoc everywhere
Modularize, don’t copy&paste!
Keep methods short!

As described before, we follow these guidelines quite consequently. Of course, there are exceptions, but these are rare and these are only accepted if there is a clear justification, as for example that a RFC function requires to implement a non-OO function module.

For the future, we also plan to modernize our development process, we would like to have feature branches as we have in our Java development using a Git repository. It looks very promissing to achieve this with abapGit and the set-up described by Ethan Jewett in his article Implementing modern practices in an ABAP development shop. For now, we use abapGit at least for having a real version history of our code.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1889139.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

NPDP究竟值不值得去考?

一、NPDP是什么&#xff1f; NPDP其实就是产品经理国际资格认证&#xff08;New Product Development Professional&#xff09;&#xff0c;是美国产品开发管理协会发起的&#xff0c;集理论、方法和实践一体&#xff0c;在新产品开发方面有一个很全面的知识体系。是国际公认…

【Python】已解决:AttributeError: ‘function’ object has no attribute ‘ELement’

文章目录 一、分析问题背景二、可能出错的原因三、错误代码示例四、正确代码示例五、注意事项 已解决&#xff1a;AttributeError: ‘function’ object has no attribute ‘ELement’ 一、分析问题背景 在Python编程中&#xff0c;AttributeError通常表明你试图访问一个对象…

气象观测站:守护天空的守望者

气象观测站的数据对于天气预报至关重要。通过对气压、温度、湿度、风速、风向等气象要素的持续监测&#xff0c;气象学家们能够分析出大气的运动规律&#xff0c;预测出未来的天气变化。这些预测信息对于人们的生产生活、交通运输、防灾减灾等方面都具有重要的指导意义。 除了天…

PLC边缘网关在实际应用中的作用-天拓四方

随着工业自动化的快速发展&#xff0c;PLC已成为工业自动化领域中不可或缺的核心设备。然而&#xff0c;随着工业物联网的兴起&#xff0c;PLC设备面临着数据集成、远程监控以及安全性等方面的挑战。为了解决这些问题&#xff0c;PLC边缘网关应运而生&#xff0c;它作为连接PLC…

【机器学习】在【Pycharm】中的实践教程:使用【逻辑回归模型】进行【乳腺癌检测】

目录 案例背景 具体问题 1. 环境准备 小李的理解 知识点 2. 数据准备 2.1 导入必要的库和数据集 小李的理解 知识点 2.2 数据集基本信息 小李的理解 知识点 注意事项 3. 数据预处理 3.1 划分训练集和测试集 小李的理解 知识点 注意事项 3.2 数据标准化 小李…

线程不安全情况之内存可见性——volatile

内存可见性 影响线程安全的原因有很多&#xff0c;内存可见性的也会引起线程不安全。 以下面的案例来看&#xff0c;线程启动后&#xff0c;t1不断进行循环&#xff0c;直到t2输入数字后改变状态&#xff0c;t1线程才会结束。 private static int count;public static void ma…

区间动态规划——最长回文子序列长度(C++)

把夜熬成粥&#xff0c;然后喝了它。 ——2024年7月1日 书接上回&#xff1a;区间动态规划——最长回文子串&#xff08;C&#xff09;-CSDN博客&#xff0c;大家有想到解决办法吗&#xff1f; 题目描述 给定一个字符串s&#xff08;s仅由数字和英文大小写字母组成&#xff0…

JavaSEJava8 时间日期API + 使用心得

文章目录 1. LocalDate2. LocalTime3. LocalDateTime3.1创建 LocalDateTime3.2 LocalDateTime获取方法 4. LocalDateTime转换方法4.1 LocalDateTime增加或者减少时间的方法4.2 LocalDateTime修改方法 5. Period6. Duration7. 格式转换7.1 时间日期转换为字符串7.2 字符串转换为…

基于YOLOv5的人脸目标检测

本文是在之前的基于yolov5的人脸关键点检测项目上扩展来的。因为人脸目标检测的效果将直接影响到人脸关键点检测的效果&#xff0c;因此本文主要讲解利用yolov5训练人脸目标检测(关键点检测可以看我人脸关键点检测文章) 基于yolov5的人脸关键点检测&#xff1a;人脸关键点检测…

FL Studio 21.0.3.3517中文破解版2024最新Keygen免费下载安装激活教程

你们是否也是音乐制作爱好者呢&#xff1f;如果是&#xff0c;那就仔细阅读文章收集对自己有帮助的操作技巧吧~~ FL Studio 21.2.3 Win-安装包下载如下: https://wm.makeding.com/iclk/?zoneid55981 FL Studio 21 .2.3Mac-安装包下载如下: https://wm.makeding.com/iclk/?…

Unity 3D软件下载安装;Unity 3D游戏制作软件资源包获取!

Unity3D&#xff0c;它凭借强大的功能和灵活的特性&#xff0c;在游戏开发和互动内容创作领域发挥着举足轻重的作用。 作为一款顶尖的游戏引擎&#xff0c;Unity3D内置了先进的物理引擎——PhysX。这一物理引擎堪称业界翘楚&#xff0c;能够为开发者提供全方位、高精度的物理模…

东软医疗获通用技术集团战略投资,共创中国医疗设备产业未来

近日&#xff0c;通用技术集团所属资本公司联合中国国有企业混合所有制改革基金有限公司与东软医疗系统股份有限公司&#xff08;下称“东软医疗”&#xff09;签署投资协议&#xff0c;战略投资东软医疗。 作为央企和民企携手发展高端医疗设备产业的具体实践&#xff0c;此次…

有什么事在windows上显的顺理成章,在Linux上就很令人费解?

在开始前刚好我有一些资料&#xff0c;是我根据网友给的问题精心整理了一份「Linux的资料从专业入门到高级教程」&#xff0c; 点个关注在评论区回复“888”之后私信回复“888”&#xff0c;全部无偿共享给大家&#xff01;&#xff01;&#xff01;可以假定一个几乎恒久不变的…

基于Java的音乐网站系统01239

目 录 摘要 1 绪论 1.1 研究背景 1.2系统开发目标、意义 1.3研究内容 2 相关技术介绍 2.1 MySQL数据库 2.2 Java编程语言 2.3 SpringBoot框架介绍 3 系统需求分析与设计 3.1 可行性分析 3.1.1 技术可行性分析 3.1.2 经济可行性分析 3.1.3 法律可行性分析 3.2 需…

已成功与服务器建立连接,但是在登录过程中发生错误。(provider: SSL提供程序,error:0-证书链是由不受信任的颁发机构颁发的。)

已成功与服务器建立连接&#xff0c;但是在登录过程中发生错误。(provider: SSL提供程序,error:0-证书链是由不受信任的颁发机构颁发的。) 在连接SQL Server2008R2数据库时发生错误。 连接字符串&#xff1a;server127.0.0.1;uidsa;pwd1;databasedb; 解决办法&#xff1a; 方…

Python--线程基础

相关概念 线程是"轻量级进程",是计算机中CPU进行任务调度的最小单位。 线程属于进程的一部分,一个线程只能属于一个进程,而一个进程可以有多个线程,且至少有一个线程。 每个进程开始的创建的时候,都会随之创建一个主线程。 进程负责分配和隔离资源(CPU, 内存…

2002-2022年各省老年人口抚养比(人口抽样调查)数据

2002-2022年各省老年人口抚养比(人口抽样调查)数据 1、时间&#xff1a;2002-2022年 2、指标&#xff1a;老年人口抚养比 3、来源&#xff1a;国家统计局、统计年鉴 4、范围&#xff1a;31省&#xff0c; 5、缺失情况&#xff1a;无缺失&#xff0c;其中2010年的值取2009、…

人脸嵌入向量提取计算

本文将讨论如何使用开源中提供的预训练模型从图像中提取人脸编码或人脸嵌入。我还附上了代码&#xff0c;请参阅此 git仓库。 NSDT工具推荐&#xff1a; Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器 - REVIT导出…

灌区量测水管理系统是如何实现灌区节水?

随着全球水资源日益紧张&#xff0c;节水已成为农业生产中不可忽视的一环。在灌区管理中&#xff0c;量测水管理系统以其精准的数据监测和科学的灌溉管理&#xff0c;为实现灌区节水提供了强有力的技术支持。 灌区量测水管理系统是一套集成了自动化监测、数据传输、数据分析和…

基于星火大模型的群聊对话分角色要素提取挑战赛|#AI夏令营#Datawhale#夏令营-Lora微调与prompt构造

赛题连接 https://challenge.xfyun.cn/topic/info?typerole-element-extraction&optionphb Datawhale Al夏令营 零基础入门大模型技术竞赛 数据集预处理 由于赛题官方限定使用了星火大模型&#xff0c;所以只能调用星火大模型的API或者使用零代码微调 首先训练数据很少…