重構現有代碼:Refactoring

来源:https://www.cnblogs.com/zhaowei520/archive/2019/04/16/10710168.html
-Advertisement-
Play Games

重構現有代碼:Refactoring 1.WHY SHOULD WE REFACTOR? 1.Refactoring Improves the Design of Software Without refactoring, the internal design—the architecture—o ...


重構現有代碼: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.

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、簡介 ntop是一家以技術為驅動的公司,用戶多為個人和小公司,主要經營四款產品:Packet Capture、Traffic Recording、Network Probe、Traffic Analysis。 二、packet Capture 網路抓包使用具有PF_RING的商用硬體進行線速數據 ...
  • 系列鏈接地址: 深入理解設計模式 系列目錄 一、產品汪的神助攻,代碼狗的安慰劑 定義:設計模式,指的是一個場景(context)下的一種解決方法(Solution),只要大家都認可某種模式,那麼就只需要很短的一個名字,就可以代替很多很多的語言和文字交流,如果你覺得設計模式降低生產效率,那隻能說你在這 ...
  • 類是不是越小越好?最近在讀John Ousterhout的《A Philosophy of Software Design》,感到作者文筆流暢,書中內容具有啟發性。這裡摘要一部分內容,以供參考、學習。 本文鏈接:https://www.cnblogs.com/hhelibeb/p/10708951. ...
  • 多態的概述多態是繼封裝、繼承之後,面向對象的第三大特性。現實事物經常會體現出多種形態,如學生,學生是人的一種,則一個具體的同學張三既是學生也是人,即出現兩種形態。 Java作為面向對象的語言,同樣可以描述一個事物的多種形態。如Student類繼承了Person類,一個Student的對象便既是Stu... ...
  • URL監控埋點作用 一個http請求來了之後,會自動打點,能夠記錄每個url的訪問情況,並將以此請求後續的調用鏈路串起來,可以在cat上查看logview 可以在cat Transaction及Event 頁面上都看到URL和URL.Forward(如果有Forward請求的話)兩類數據;Trans ...
  • 寫代碼也可以進行資源整合,實現相應的功能,達到預期的目標即可。 ...
  • 前言 最近在做工作流的事情,正好有個需求,要添加一個附件上傳的功能,曾找過不少上傳插件,都不是特別滿意。無意中發現一個很好用的開源web文件管理器插件 elfinder,功能比較完善,社區也很活躍,還方便二次開發。 環境搭建 | 軟體 | 地址| | | | | SpringBoot| https: ...
  • 圖片比對 昨天的博客已經將圖片存儲到了本地,今天要做的第一件事情,就是需要在兩張圖片中進行比對,將圖片缺口定位出來 缺口圖片 完整圖片 計算缺口坐標 對比兩張圖片的所有RBG像素點,得到不一樣像素點的x值,即要移動的距離 極驗證對於用戶行為檢測是有專門的演算法的,找到一篇比較老的文章 https:// ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...