天天看點

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 code refacting using string template

A better solution could be using string template instead. A draft code looks like below:

A code refacting using string template

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.