Building Maintainable Software-java篇之Write Code Once

Building Maintainable Software-java篇之Write Code Once

Number one in the stink parade is duplicated code.
—Kent Beck and Martin Fowler,
Bad Smells in Code


Guideline:

• Do not copy code.
• Do this by writing reusable, generic code and/or calling existing methods instead.
• This improves maintainability because when code is copied, bugs need to be €xed at multiple places, which is inefficient  and error-prone.


Copying existing code looks like a quick win—why write something anew when it already exists? The point is: copied code leads to duplicates, and duplicates are a problem. As the quote above indicates, some even say that duplicates are the biggest software quality problem of all.


Motivation

To understand the advantages of a codebase with little duplication, in this section we discuss the effects that duplication has on system maintainability.


Duplicated Code Is Harder to Analyze



If you have a problem, you want to know how to fix it. And part of that “how” is where to locate the problem. When you are calling an existing method, you can easily find the source. When you are copying code, the source of the problem may exist elsewhere as well. However, the only way to find out is by using a clone detection tool. A well-known tool for clone detection is CPD, which is included in a source code analysis tool called PMD. CPD can be run from inside Eclipse as well as from Maven.




Duplicated Code Is Harder to Modify



All code may contain bugs. But if duplicated code contains a bug, the same bug appears multiple times. Therefore, duplicated code is harder to modify; you may need to repeat bug fixes multiple times. This, in turn, requires knowing that a fix has to be made in a duplicate in the first place! This is why duplication is a typical source of socalled regression bugs: functionality that has worked normally before suddenly stops working (because a duplicate was overlooked).
The same problem holds for regular changes. When code is duplicated, changes may need to be made in multiple places, and having many duplicates makes changing a codebase unpredictable.




How to Apply the Guideline



To avoid the problem of duplicated bugs, never reuse code by copying and pasting existing code fragments. Instead, put it in a method if it is not already in one, so that you can call it the second time that you need it. That is why, as we have covered in the previous chapters, the Extract Method refactoring technique is the workhorse that solves many duplication problems.


The Extract Superclass Refactoring Technique



Common Objections to Avoiding Code Duplication




读书笔记:

Building Maintainable Software: Ten Guidelines for Future-Proof Code


by Joost Visser


Copyright © 2016 Software Improvement Group, B.V. All rights reserved.


Printed in the United States of America.


Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.


O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are


also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/


institutional sales department: 800-998-9938 or corporate@oreilly.com.


Acquisitions Editor: Rachel Roumeliotis


Editor: Nan Barber


Production Editor: Matthew Hacker


Copyeditor: Rachel Monaghan


Proofreader: Marta Justak


Indexer: WordCo Indexing Services, Inc.


Interior Designer: David Futato


Cover Designer: Randy Comer


Illustrator: Rebecca Demarest


February 2016: First Edition


Revision History for the First Edition


2016-01-25: First Release


See  http://shop.oreilly.com/product/0636920049159.do

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dreamer who

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值