Extract Method is a refactoring operation that provides an easy way to create a new method from a code fragment in an existing member.
Using Extract Method, you can create a new method by extracting a selection of code from inside the code block of an existing member. The new, extracted method contains the selected code, and the selected code in the existing member is replaced with a call to the new method. Turning a fragment of code into its own method lets you quickly and accurately reorganize code for better reuse and readability.
Extract Method has the following benefits:
1. Encourages best coding practices by emphasizing discrete, reusable methods.
2. Encourages self-documenting code through good organization.
3. When descriptive names are used, high-level methods can read more like a series of comments.
4. Encourages the creation of finer-grained methods to simplify overriding.
5. Reduces code duplication.
實例說明
我們在Controller中,會撰寫新增、修改、刪除等method,其中在新增與修改的部份,會將前端form bean的value轉成entity class,然後再呼叫insert or update method,其code snippets如下:
我們在Controller中,會撰寫新增、修改、刪除等method,其中在新增與修改的部份,會將前端form bean的value轉成entity class,然後再呼叫insert or update method,其code snippets如下:
Extract Method Refactoring三部曲
1. 選取要進行refactor的部分
2. 按右鍵-->Refactor-->Extract Method
3. 給予新的method name --> OK
檢查Refactor後的結果
Eclipse將原本set的動作抽離成一個獨立的method
insert與update的method中,原本重複的動作只要用covertBeanToEntity此method取代即可,避免code duplication
No comments:
Post a Comment