Showing posts with label Refactoring. Show all posts
Showing posts with label Refactoring. Show all posts

Sunday, June 9, 2024

[Refactoring] Tools

 

Java 環境


  1. SonarQube
    • 支援多種程式語言,包括Java。可以分析程式碼質量,檢測內聚性和耦合性,提供詳細的報告和建議。
  2. JDepend
    • 專門針對Java程式碼的工具,可以分析package之間的依賴關係,從而了解模組的耦合性。
  3. Structure101
    • 提供可視化分析,幫助識別程式碼中的結構問題,同時可以評估內聚性和耦合性。

C#/.NET 環境


  1. NDepend
    • 專門針對.NET程式碼的分析工具,能夠詳細檢測內聚性和耦合性,並提供改善的建議。
  2. ReSharper
    • 錶類似功能的插件,可以幫助在C#程式中識別問題並提供修正建議。

Python 環境


  1. Pylint
    • 這是一個靜態程式碼分析工具,可以檢測 Python 程式碼中的問題,並提供相關的內聚性和耦合性分析。
  2. Radon
    • 用於分析Python代碼複雜度的工具,能夠提供測量內聚性和耦合性相關指標的信息。

C++ 環境


  1. CppDepend
    • 為C++專門設計,這個工具可以提供內聚性和耦合性的詳細報告,並含有可視化分析功能。

通用工具


  1. Understand
    • 支援多種程式語言,這是一個廣泛使用的靜態分析工具,提供內聚性和耦合性分析功能。
  2. Metric
    • 這是一套框架和工具,可以用於評估多種程式語言的內聚性和耦合性,特別適合大型專案。

Friday, January 29, 2021

Code Deodorant

Reference

https://refactoring.com/catalog/

https://www.jyt0532.com/toc/refactoring/

1. Long Function

Extract Function

Replace Temp with Query

Introduce Parameter Object

Preserve Whole Object

Replace Function with Command

Decompose Conditional

Replace Conditional with Polymorphism

Split Loop (重構與效能) 有時候為了讓程式碼更容易維護, 犧牲效能

2. Large Class
3. Long Parameter List

4. Data Clumps

5. Primitive Obsession

Replace Type Code with Subclasses

Replace Conditional with Polymorphism

Extract Class

Introduce Parameter Object

6. Repeated Switch

Replace Conditional with Polymorphism

Remove Flag Argument

7. Temporary Field

Extract Class

8. Alternative Classes with Different Interfaces

Move Function

Extract Superclass

9. Refused Bequest

Replace Superclass with Delegate (延伸)

10. Divergent Change

Move Function

Extract Function

Extract Class

11. Shotgun Surgery

Move Function

Combine Functions into Class

Combine Function into Transform 

Split Phase

Inline Function

12. Parallel Inheritance Hierarchies

Move Method

Move Field

13. Duplicate Code

Extract Function

14. Lazy Class

Inline Function

Inline Class

Collapse Hierarchy

15. Speculative Generality

Inline Function

Inline Class 

Change Function Declaration

16. Data Class

Remove Setting Method (this value can't change)

Move Function

Extract Function

Split Phase

17. Dead code

Remove Unreachable Code

18. Feature Envy

Move Function

Extract Function

19. Message Chains

Hide Delegate

Extract Function

Move Function

20. Middle Man

Inline Function

Replace Superclass with Delegate

Replace Subclass with Delegate

21. Inappropriate Intimacy

Move Field

Extract Class

Hide Delegate

Replace Inheritance with Delegation (https://learnku.com/docs/99-software-pattern/delegation-pattern/12018)

22. Comments

Extract Function

Change Function Declaration

23. Global Data

24. Mutable Data

Encapsulate Variable

Extract Function 

Remove Setting Method (this value can't change)

25. Mysterious Name

Change Function Declaration

26. Incomplete Library Class 

Move Method

27. Loops

28. Insider Trading

Move Function

Move Field

Replace Subclass with Delegate




Wednesday, January 20, 2021

Bad Smells in Code

Bloaters (程式碼已經成長得過於龐大,大於能夠妥善掌握發揮的幅度)

1. Long Function (過長函式)

2.  Large Class (過大類別)

3.  Long Parameter List (過長參數列)

4.  Data Clumps (資料泥團) 

2 個 classes 內有相同欄位、相同參數

 5. Primitive Obsession (基本型別偏執)

沒有將基本型別轉化成小物件,小物件如,Date class 

Tool Abusers (濫用物件導向設計,也就是說沒有完全發揮物件導向設計的特性)

6. Repeated Switch (重複的切換邏輯)

同樣的 switch 述句散佈於不同地點,添加一個新的 case 子句,必須找到所有 switch 述句並修改它們

7. Temporary Field (令人迷感的暫時欄位)

 某個 instance 變數僅為某個特定情勢而設

8. Alternative Classes with Different Interfaces (異曲同工的類別)

 兩個函式做同一件事,卻有著不同的署名式

9. Refused Bequest (被拒絕的遣贈)

 只繼承父類別某個抽象類別

Change Preventers (小部分的修訂會連動到大範圍的修訂) 

10. Divergent Change (發散式修改)

相同 class 一旦新增 A 功能要改 3 個函式,一旦新增 B 功能要改 4 個函式

11. Shotgun Surgery (霰彈式修改)

 如果每遇到新增功能,需要修改散佈四處的 classes 程式碼

 12. Parallel Inheritance Hierarchies (平行繼承體系)

 某個 class 增加 subclass,另一個 class 也相同必須增加 subclass

Dispensables (程式碼中多餘的部分,應該要被移出 source code) 

13. Duplicate Code (重複的程式碼)

14.  Lazy Class (冗員類別)

15. Speculative Generality (夸夸其談未來性)

 預留了太多未來可能會用到的擴充點

16.  Data Class (純稚的資料類別)

 沒有將資料欄位封裝起來

 17. Dead Code (無作用的程式碼)

 The Couplers (Couplers 說明程式碼之間存在強烈相依性、高度的耦合情形)

18. Feature Envy (依戀情結)

 某個 class 常呼叫其它 class 的函式

19. Message Chains (過度耦合的訊息鏈)

 當用戶向一個物件索求 (request) 另一個物件,然後再向後者索求另一個物件…

20. Middle Man (中間轉手人)

 某個 class 介面有一半的函式都委託給其他 class,這樣就是過度運用

 21. Inappropriate Intimacy (狎暱關係)

 兩個 classes 過於親密,花費太多時間去探究彼此的資料

其它 7 個 code smells

22. Comments (過多的註釋)

23. Global Data (全域資料) 

24. Mutable Data (可變資料)

25. Mysterious Name (神秘的名稱)

26. Incomplete Library Class (不完美程式庫類別)

27. Loop (迴圈)

 first-class function

28. Insider Trading (內幕交易)

 模組間過度交換資料會導致更多耦合

Refactoring index

Introduction

Bad Smells in Code

Code Deodorant

Reference:

http://mikamantyla.eu/BadCodeSmellsTaxonomy.html

n8n index

 【n8n免費本地端部署】Windows版|程式安裝x指令大補帖  【一鍵安裝 n8n】圖文教學,獲得無限額度自動化工具&限時免費升級企業版功能