关于refactoring

  重构让代码变得有价值而不是负担。通过时机、切入点、原则三个词语来简单了解一下重构。

  什么时候我们要重构,逻辑重复、程序复杂不好理解、添加功能时不好修改。

 Method:

相同的代码

过长的参数

不好理解的方法名

复杂的算法

  Class:

相似的method

过大的class

临时、相同的field

面向过程化

职责不清晰

纯数据class

  Organizing 

一个class引发多个class修改(divergent change)

一个class受多个class变化的影响(shotgun surgery)

过度的继承

过度delegation

过度消息耦合(message chains)

当我们遇到这些问题重构时,可参考哪些经验和原则:

Method

函数名表达意图(代替注释)、职责明确(一个函数一个意图)、参数简单(可传递对象)、减少全局变量的使用、局部变量再复值、去除重复表达式、精简算法

简化条件表达式

Between Object

单一职责 (将method、field转移到合适的类)、使用封装、减少耦合、去除不必要关联(在feild中去除)

加委托(隐藏细节、通过server代理或委托类)、去委托(委托泛滥、继承代替委托)

扩展类库(提炼子类、提炼超类)、用接口代替继承 委托代替继承、使用多态

避免纯数据类、将数据转换成对象

使用设计模式

在开发程序是我们需要先设计、熟悉重构避免重构。

时间: 2024-10-08 10:07:42

关于refactoring的相关文章

taiyi_interview(Introduction To Database Refactoring)

Introduction To Database Refactoring 原文链接:by Scott W. Ambler:http://www.tdan.com/view-articles/5010/ Published: July 1, 2006 Published in TDAN.com July 2006 Material for this article was modified from Refactoring Databases: Evolutionary Database Desi

VS2015上又一必备免费插件:Refactoring Essentials

(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:Refactoring Essentials是一款用于代码分析和重构的开源免费VS2015插件,其功能丰富强大,必然会成为类似Web Essentials这样的必备插件. 之前由SharpDevelop团队开发且用于SharpDevelop这个开源IDE中的重构插件"NR6Pack"改名为"Refactoring Essentials",被Hanselm

[Project organization] - structure refactoring with TFS

/By Dylan SUN/ There are something you need to pay attention to when you make a project structure refactoring. You could NEVER delete a project in the TFS and re-add it into the place you want. In this way, you will lose all the changesets about this

[Javascript] Refactoring: Polymorphic Functions

if-statements can add serious complexity and beg for refactoring. You can use polymorphic functions to simplify your ifs and dynamically call the appropriate method. For example you have the code like: function charge(){ if(this.moive.type==="regular

【RefactoringCode】The description of the refactoring book

Last night the book named [Data Structure with Java Hubbed] was closed. When talked about the advantage that I have taken of, with the help of that book I familize myself with data structure, especially the Java Collection Framework. I think it impor

Refactoring to collection(译)

<Refactoring To Collection>#####本文是翻译Adam Wathan 的<Collection To Refactoring>的试读篇章,这篇文章内容不多,但是可以为我们Laraver使用者能更好使用collection提供了可能性,非常值得看看.虽然是试读部分,但是Wathan还是很有诚意的,试读文章还是能学到东西的,但是遗憾的是,我大概搜了下,目前好像还没有中文版,为此,我决定翻译这篇文章,让英文不太好的朋友,可以学习这篇文章. 获取试读文章:htt

[Learn Android Studio 汉化教程]第四章 : Refactoring Code

[Learn Android Studio 汉化教程]第四章 : Refactoring Code 第四章 Refactoring Code    重构代码 在Android Studio中开发,解决方案不会总是一蹴而成的.作为一个有效率的编程者,在你的开发,调试和测试中需要一些弹性以及代码重构.随着在这章中的行进,你将明白Android Studio如何产生代码:在这章里你将看到Android Studio如何重构你的代码.重构代码最大的风险是可能引入不期望的错误.通过分析某些风险重构操作的结

Top 6 Refactoring Patterns to Help You Score 80% in Code Quality

Top 6 Refactoring Patterns to Help You Score 80% in Code Quality Posted by Ajitesh Kumar / In Code Review, Software Quality / January 31, 2014 Have done several code reviews in past and found following top 5 code smells common across most of these co

BNUOJ 19297 Code Refactoring

Code Refactoring Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1087964-bit integer IO format: %lld      Java class name: Main Problem BCode RefactoringTime Limit: 2 seconds "Harry, my dream is a code waiti

BookNote: Refactoring - Improving the Design of Existing Code

BookNote: Refactoring - Improving the Design of Existing Code From "Refactoring - Improving the Design of Existing Code" by Martin Flower. BookNote: Refactoring - Improving the Design of Existing Code Duplicated Code Long Method Large Class Long