重構現有代碼: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
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...