重构现有代码:Refactoring

1.WHY SHOULD WE REFACTOR?

  1.Refactoring Improves the Design of Software

  Without refactoring, the internal design—the architecture—of software tends to decay. As people change code to achieve short-term goals, often without a full comprehension of the architecture, the code loses its structure.

  2.Refactoring Makes Software Easier to Understand

  The trouble is that when I’m trying to get the program to work, I’m not thinking about that futuredeveloper. It takes a change of rhythm to make the code easier to understand.

  Refactoring helps memake my code more readable. Before refactoring, I have code that works but is not ideally structured. A little time spent on refactoring can make the code better communicate its purpose—say more clearly what I want.

  3.Refactoring Helps Me Find Bugs

  4.Refactoring Helps Me Program Faster

  

2.WHEN SHOULD WE REFACTOR?

  1.Making It Easier to Add a Feature

  2.Making Code Easier to Understand

  3.Litter-Pickup Refactoring

  4.Planned and Opportunistic Refactoring

3.PROBLEMS WITH REFACTORING

  1.Slowing Down New Features

  2.Code Ownership

  3.Branches

4.Bad Smells in Code

  One thing we won’t try to give you is precise criteria for when a refactoring is overdue. In our experience, no set of metrics rivals informed human intuition. What we will do is give you indications that there is trouble that can be solved by a refactoring.

  You will have to develop your own sense of how many instance variables or how many lines of code in a method are too many.

  Use this chapter and the table on the inside back cover as a way to give you inspiration when you’re not sure what refactorings to do. Read the chapter (or skim the table) and try to identify what it is you’re smelling, t

  hen go to the refactorings we suggest to see whether they will help you. You may not find the exact smell you can detect, but hopefully it should point you in the right direction.

  

  1.MYSTERIOUS NAME

    One of the most important parts of clear code is good names, so we put a lot of thought into naming functions, modules, variables, classes, so they clearly communicate what they do and how to use them.

   2.DUPLICATED CODE

    If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them. Duplication means that every time you read these copies,

    you need to read them carefully to see if there’s any difference. If you need to change the duplicated code, you have to find and catch each duplication.

  3.LONG FUNCTION

    Since the early days of programming, people have realized that the longer a function is, the more difficult it is to understand.

  4.LONG PARAMETER LIST

    If you can obtain one parameter by asking another parameter for it, you can use Replace Parameter with Query (324) to remove the second parameter. Rather than pulling lots of data out of an existing data structure,

    you can use Preserve Whole Object (319) to pass the original data structure instead. If several parameters always fit together, combine them with Introduce Parameter Object (140).

    If a parameter is used as a flag to dispatch different behavior, use Remove Flag Argument (314).

  5.GLOBAL DATA

    The problem with global data is that it can be modified from anywhere in the code base, and there’s no mechanism to discover which bit of code touched it.

    Time and again, this leads to bugs that breed from a form of spooky action from a distance—and it’s very hard to find out where the errant bit of program is.

    The most obvious form of global data is global variables, but we also see this problem with class variables and singletons.

重构现有代码:Refactoring的更多相关文章

  1. 高效重构 C++ 代码

    引言 Martin Fowler的<重构:改善既有代码的设计>一书从2003年问世至今已有十几年时间了,按照计算机领域日新月异的变化速度,重构已经算是一门陈旧的技术了.但是陈旧并不代表不重 ...

  2. 实际案例:在现有代码中通过async/await实现并行

    一项新技术或者一个新特性,只有你用它解决实际问题后,才能真正体会到它的魅力,真正理解它.也期待大家能够多分享解一些解决实际问题的内容. 在我们遭遇“黑色30秒”问题的过程中,切身体会到了异步的巨大作用 ...

  3. Visual Studio 2013新建工程导入现有代码文件夹并且保持目录结构

    本文提供了一个在Windows环境下使用Visual Studio 2013编辑现有源代码并且保持目录结构的方法.本文使用VS2013中文社区版做示例(本版本为免费版,可在VS官网下载),其他版本的V ...

  4. AIDE支持实时错误检查、代码重构、代码智能导航、生成APK

    AIDE是一个Android Java集成开发环境,可以在Android系统内进行Android软件和游戏的开发.它不仅仅是一个编辑器,而是支持编写-编译-调试运行整个周期,开发人员可以在Androi ...

  5. 在现有代码中通过async/await实现并行

    在现有代码中通过async/await实现并行 一项新技术或者一个新特性,只有你用它解决实际问题后,才能真正体会到它的魅力,真正理解它.也期待大家能够多分享解一些解决实际问题的内容. 在我们遭遇“黑色 ...

  6. JAVAEE——BOS物流项目05:OCUpload、POI、pinyin4J、重构分页代码、分区添加、combobox

    1 学习计划 1.实现区域导入功能 n OCUpload一键上传插件使用 n 将文件上传到Action n POI简介 n 使用POI解析Excel文件 n 完成数据库操作 n 使用pinyin4J生 ...

  7. 【重构】 代码的坏味道总结 Bad Smell (一) (重复代码 | 过长函数 | 过大的类 | 过长参数列 | 发散式变化 | 霰弹式修改)

    膜拜下 Martin Fowler 大神 , 开始学习 圣经 重构-改善既有代码设计 . 代码的坏味道就意味着需要重构, 对代码的坏味道了然于心是重构的比要前提; . 作者 : 万境绝尘 转载请注明出 ...

  8. 重构与模式(Refactoring to Patterns)-读书笔记

    第一章 ☛过度设计:是指代码的灵活性和复杂性超出所需. 第二章 ☛重构是一种保持行为的转换. 第三章 ☛每一个模式都是由三部分组成的规则,他表达的是某一环境,一个问题以及解决问题的方案之间的关系. ☛ ...

  9. 重构Java代码的既有设计-影片出租店

    案例:计算每位顾客的消费金额并打印详细信息.顾客租赁了哪些影片,租期多长,根据租赁时间和影片类型计算出费用.影片分为3类:儿童片,新片,普通片.此外需计算该顾客的积分. Movie: public c ...

随机推荐

  1. jmeter接口测试-线程组设置(断言失败后用例停止执行)

    问题描述: jmeter跑接口用例的时候,其中一条用例的对断言失败后,后面的用例都不执行了! 解决思路1: 考虑应该有地方设置,在菜单栏找了半天没找到,百度也没有查到 解决思路2: jmeter源码导 ...

  2. codeblock 生成和使用makefile

    下载cbp2make 文件名:cbp2make-stl-rev138.tar.gz 里面有个cbp文件用codeblock打开,编译,生成的bin目录下有个执行文件. 使用命令生成Makefile . ...

  3. 微信小程序支付+php后端

    最近在做自有项目后端用的是thinkphp5.1框架,闲话不说直接上代码 小程序代码 wxpay: function(e){ let thisid = e.currentTarget.dataset. ...

  4. 用系统默认mail服务实现邮件发送

    用系统默认mail服务实现邮件发送 1.操作步骤 第一步:设备服务器发送邮件要用的,邮箱地址,账号密码 编辑/etc/mail.rc vim /etc/mail.rc 在文件的结尾追加,账号信息配置 ...

  5. 使用css让动态容器按固定宽高比显示

    需求:页面上有一个div的宽度是随着屏幕宽度的改变而改变的,但其宽高比始终是2:1,也就是当宽度是1000px时,高度为500px 分析:无论浏览器窗口如何改变,始终要让目标元素的宽高比保持2:1,我 ...

  6. Storm实现实时大数据分析(storm介绍,与Hadoop比较,)

    一.storm与Hadoop对比 Hadoop: 全量数据处理使用的大多是鼎鼎大名的hadoop或者hive,作为一个批处理系统,hadoop以其吞吐量大.自动容错等优点,在海量数据处理上得到了广泛的 ...

  7. 利用IDM工具下载ESA上的Sentinel数据

    由于美国政府关门,NASA,USGS,NOAA等机构中的非核心部门也都放假了,暂时无法提供Sentinel数据下载,而直接从ESA下载数据比蜗牛上山都慢,幸好发现了IDM工具. 利用浏览器或wget工 ...

  8. Git常用命令(一)

    转自2.1 Git 基础 - 获取 Git 仓库 获取帮助 `$ git help <verb> $ git <verb> --help $ man git-<verb& ...

  9. jquery调用iframe里面的方法

    $(window.parent.document).contents().find("#iframename")[0].contentWindow.iframefunction() ...

  10. Monkey测试简介

    1.Monkey测试简介monkey是安卓命令行工具,它向系统发送伪随机的用户事件,例如:按键的输入.触摸屏的输入.手势输入等操作来对设备上的程序进行压力测试,检测程序多久的时间会发生异常.因此,mo ...