A code refacting using string template
Created by Jerry Wang, last modified on Jun 23, 2016
The original code before refact:
There are some fields in DDIC structure CRMT_SMS_STATUS_KEY which could be used for deletion.
Currently three fields are supported: sms_uuid, phone_number and resend_times.
Current implementation is to evaluate all possible status of the three fields, whether they are intial or not.
In theory support you have n fields to support for deletion case the combination would be . If we still follow the current design, the code could not be sustainable: more and more IF … ELSEIF has to be added.
A better solution could be using string template instead. A draft code looks like below:
FORM delete USING is_status_key TYPE CRMT_SMS_STATUS_KEY.
data: lv_statement TYPE string.
CHECK is_status_key-sms_uuid IS NOT INITIAL.
lv_statement = | SMS_UUID EQ '{ IS_STATUS_KEY-SMS_UUID }' |.
IF is_status_key-phone_number IS NOT INITIAL.
lv_statement = lv_statement && | AND phone_number = '{ is_status_key-phone_number }'|.
ENDIF.
IF is_status_key-resend_times IS NOT INITIAL.
lv_statement = lv_statement && | AND resend_times = '{ is_status_key-resend_times }'|.
ENDIF.
" IF is_status_key-XXXX IS NOT INITIAL.
" lv_statement = lv_statement && | AND XXXX = '{ is_status_key-XXXX }'|.
" ENDIF.
TRY.
DELETE FROM crmd_sms_status WHERE (lv_statement).
CATCH cx_root INTO DATA(cx_root).
"" DO ERROR HANDLING HERE
ENDTRY.
ENDFORM.
The advantage is, if more fields are to be supported, you could simply add the code marked with bold, without having to code complex IF… ELSEIF statement. However, since now we use dynamical SQL statement instead, make sure to avoid the potential SQL injection by escaping all input fields of CRMT_SMS_STATUS_KEY via method cl_abap_dyn_prg=>escape_quotes.
相关文章
- Testing - 【转】代码检视列表(Code Review List)
- [Angular 2] 8. Better ES5 Code
- C++ code:string stream(string流)
- 用Visual Studio Code写Node和调试代码
- webpack 利用Code Splitting 分批打包、按需下载
- [AWS - Monitoring and Troubleshooting] 5.1 Write code that can be monitored
- [Tools MarkDown] Create a Diff in Markdown to Show What Has Changed in a Code Snippet
- [React] Asynchronously Load webpack Bundles through Code-splitting and React Suspense
- Entity Framework Code First学习系列目录
- uni-app:微信小程序登录:获取code时返回the code is a mock one(hbuilderx 3.7.3)
- 关于Visual Studio Code里进行SAP UI5开发的代码自动完成功能
- 如何处理Angular项目在Visual Studio Code打开报关于@Decorators的警告信息
- android.database.sqlite.SQLiteException: no such table: xxx (code 1 SQLITE_ERROR): , while compiling
- Auto.js Pro如何连接VS Code插件
- 【Leet Code】String to Integer (atoi) ——常考类型题
- Command "python setup.py egg_info" failed with error code 1
- ACR Code Pacs
- Mac 下 visual studio code 编辑器 设置为中文
- 1.2、基于增量式生成遮挡与对抗抑制的行人再识别(Code Analysis)
- 【代码管理】Ubuntu 2204 下 VS Code 无法输入中文
- 【区块链技术开发】Vs Code 创建、测试和部署Helloworld智能合约的Truffle示例项目(保姆级别教程)
- .net 6用EF Core基本创建表 code first