Intro Refactor Functions I
Intro Refactor Functions I
Intro
看到一個function,嘗試用三分鐘想理解程式碼的含意。
如果在三分鐘內無法理解時,代表這個function需要進
行重構。
1.提取Private method
2.重新命名有意義的名稱
3.code restructuring
以下是進行照上面三步驟整理後的程式碼,效果是
三分鐘內可理解程式涵義。
example:
1 2 3 4 5 6 7 8 9 10 11 12
| public static String renderPageWithSetupsAndTeardowns (PageData pageDate, boolean isSuite) throws Exception { boolean isTestPage = pageData.hasAttribute("Test"); if (isTestPage) { WikiPage testPage = pageData.getWikiPage(); StringBuffer newPageContent = new StringBuffer(); includeSetupPages(testPage, newPageContent, isSuite); newPageContent.append(pageData.getContent()); includeTeardownPages(testPage, newPageContent, isSuite); pageData.setContent(newPageContent.toString()); } return pageData.getHtml(); }
|