提升代码可维护性:重构技巧与坏味道识别

需积分: 0 1 下载量 177 浏览量 更新于2025-01-01 收藏 54KB DOC 举报
在"学习oo好榜样"这份文档中,作者分享了关于面向对象(OO)编程中的关键概念,特别是针对代码质量与重构的一些原则和技巧。文档的核心关注点包括BadSmells(代码异味)和Refactoring(重构),这两个概念对于软件开发人员来说至关重要。 首先,作者引用了马丁·福勒(Martin Fowler)的名言,强调了写出易于人类理解的代码的重要性,这不仅是优秀程序员的标准,也是保持代码可维护性和可扩展性的基础。BadSmells是指那些违反软件设计原则,降低代码质量的行为或模式,如重复代码和过长函数。 1. **重复代码**(Duplicate Code)是BadSmells之一。它可能导致代码冗余,增加理解和维护的困难。如果多个部分的代码几乎相同,只是在细节上有所差异,这种重复会使得任何一处的修改都可能波及多处,增加了出错的可能性。作者举例提到`filename.trim().equals("")`这样的代码片段,指出一旦需要改动,就需要在多个位置进行同步更新,降低了代码的可维护性。根据经验,当重复代码达到一定程度,比如需要使用查找工具(如Ctrl-C/Ctrl-V)才能找到所有相关的地方,就应当考虑重构,如通过ExtractMethod(提取方法)或ExtractClass(提取类)等技术减少重复。 2. **过长函数**(Long Method)同样属于BadSmells。过长的函数往往难以理解和测试,降低了代码的可读性。作者建议将函数长度控制在10行以内,确保每个函数的功能单一,这样有助于提高代码的可读性和模块化。短小且功能明确的函数能够清晰地展现逻辑,便于他人阅读和维护。 在面对这些BadSmells时,文档强调了Refactoring的价值,即通过改进代码结构而不改变其外部行为,来提升代码质量。通过像ExtractMethod、Pullup Method和FormTemplateMethod等重构技术,可以有效地消除重复代码,提高代码的可重用性和一致性,从而提高整个系统的健壮性和可维护性。 "学习oo好榜样"文档提供了实用的指导,帮助开发者识别并修复代码中的不良模式,通过遵循良好的OO原则和持续的重构实践,以实现高效、易于理解且易于维护的软件系统。这对于在实际工作中优化设计和解耦具有显著的帮助。

编译报错 CREATE OR REPLACE PROCEDURE get_dev_cs IS sql_tem VARCHAR2(4000); v_dt NUMBER; BEGIN ----om_subscriber_yyyymm sql_tem := 'truncate table ycyd.Subscriber_cs'; EXECUTE IMMEDIATE sql_tem; FOR v_dt IN (SELECT to_number(substr(table_name, '15')) db FROM sys.all_tables a WHERE a.owner = upper('jour1') AND a.table_name LIKE upper('om_subscriber_202___') AND substr(table_name, '15') > 202205) LOOP -- 循环往b里添加数据 sql_tem := ' INSERT INTO ycyd.Subscriber_cs SELECT a.order_id, a.done_date, a.cust_id, a.subscriber_ins_id, a.access_num FROM jour1.om_subscriber_' || v_dt.db || ' a WHERE a.action = 0 AND a.is_usim = 1'; -- 执行sql EXECUTE IMMEDIATE sql_tem; END LOOP; ----om_order_yyyymm sql_tem := 'truncate table ycyd.order_cs'; EXECUTE IMMEDIATE sql_tem; COMMIT; FOR v_dt IN (SELECT to_number(substr(table_name, '15')) db FROM sys.all_tables a WHERE a.owner = upper('jour1') AND a.table_name LIKE upper('om_subscriber_202___') AND substr(table_name, '15') > 202205) LOOP -- 循环往b里添加数据 sql_tem := ' INSERT INTO ycyd.order_cs SELECT order_id, dev_id FROM JOUR1.om_order_' || v_dt.db || ' b'; -- 执行sql EXECUTE IMMEDIATE sql_tem; END LOOP; COMMIT; ---dev sql_tem := 'truncate table ycyd.yd_order_dev_3056'; EXECUTE IMMEDIATE sql_tem; -- 添加数据 sql_tem := ' INSERT INTO ycyd.yd_order_dev_3056 SELECT a.order_id, a.done_date, a.cust_id, a.subscriber_ins_id, a.access_num, c.dev_id, c.dev_name, d.code, c.dev_busi_code, c.corp_org, c.company_name, c.department_code, c.department_name, c.remarks FROM ycyd.Subscriber_cs a LEFT JOIN ycyd.order_cs b ON a.order_id = b.order_id LEFT JOIN params1.sec_developer c ON b.dev_id = c.dev_id LEFT JOIN (SELECT DISTINCT oo.code,s.bill_id FROM params1.sec_operator oo, params1.sec_staff s WHERE s.staff_id = oo.staff_id AND oo.state = 1 AND oo.staff_id NOT in ('123402933','123395474','123404209')) d ON c.dev_phone = d.bill_id WHERE c.corp_org = 3056'; -- 执行sql EXECUTE IMMEDIATE sql_tem; COMMIT; END get_dev_cs;

158 浏览量

String singleSend = (String) map.get("singleSend"); String expressProductCode = (String) map.get("expressProductCode"); LinkedHashMap<String,String> senderInformation = (LinkedHashMap<String,String>)map.get("senderInformation"); String senderName = senderInformation.get("label"); String senderPhone = senderInformation.get("value"); String productName = (String) map.get("productName"); if("1".equals(singleSend)) { for(Map<String, String> oo : list ) { String receiverName = oo.get("receiverName"); String receiverPhone = oo.get("receiverPhone"); String contactAddress = oo.get("contactAddress"); String orderNos = oo.get("orderNo"); OrderApiCreate orderApiCreate = genParam(expressProductCode,productName,senderName,senderPhone,receiverName,receiverPhone,contactAddress,"1", orderNos); String ret = apiUtil.createOrder(orderApiCreate); LoginUser loginUser = SecurityUtils.getLoginUser(); OrderInfo orderinfo = new OrderInfo(); orderinfo.setUpdateBy(loginUser.getUsername()); orderinfo.setApiOrderNo(orderApiCreate.getOrderNumber()); if(StringUtils.isNotEmpty(ret)) { orderinfo.setSendError(ret); orderinfo.setSendState("3"); orderinfo.setSendCount(0); } else { orderinfo.setSendState("2"); } orderService.updateApiSendInfo(new String[]{String.valueOf(oo.get("uuid"))}, orderinfo); } } else { String receiverName = (String) map.get("receiverName"); String receiverPhone = (String) map.get("receiverPhone"); String contactAddress = (String) map.get("contactAddress"); String productNumber = String.valueOf(uuids.length); String orderNos = list.stream().map(item->String.valueOf(item.get("orderNo"))).collect(Collectors.joining(",")); OrderApiCreate orderApiCreate = genParam(expressProductCode,productName,senderName,senderPhone,receiverName,receiverPhone,contactAddress,productNumber, orderNos); String ret = apiUtil.createOrder(orderApiCreate); LoginUser loginUser = SecurityUtils.getLoginUser(); OrderInfo orderinfo = new OrderInfo(); orderinfo.setUpdateBy(loginUser.getUsername()); orderinfo.setApiOrderNo(orderApiCreate.getOrderNumber()); if(StringUtils.isNotEmpty(ret)) { orderinfo.setSendError(ret); orderinfo.setSendState("3"); orderinfo.setSendCount(0); } else { orderinfo.setSendState("2"); } orderService.updateApiSendInfo(uuids, orderinfo); } apiUtil.getOrderSendState(); return AjaxResult.success(result);优化这段代码

2023-06-27 上传