新手C#代碼簡潔性—函數篇

來源:互聯網
上載者:User

標籤:故事   button   新手   一個   alt   images   htmlhelp   UI   選擇性參數   

1.從Html代碼抽取MVC HtmlHelper


<td class = "noborder nopaddingv">
<div class = "aligncenter">
<a href="/Home/Index">
<img
src="../../Resouces/Images/Agile/Menu/Campass48.png" alt = "網站地圖"
title = "網站地圖" class = "imagelink"/></a><br />
網站地圖
</div>
</td>
<td class = "noborder nopaddingv">
<div class = "aligncenter">
<a href="/Home/Index" target = "_blank">
<img
src="../../Resouces/Images/Agile/Help/Help48.png" alt = "協助" title = "
協助" class = "imagelink"/></a><br />
協助
</div>
</td>

改後:

@Html.MenuImageButtonV("網站地圖", false, "/Home/Index",
"../../Resouces/Images/Agile/Menu/Campass48.png")
@Html.MenuImageButtonV("協助", true, "/Home/Index",
"../../Resouces/Images/Agile/Help/Help48.png")
這種技法:當兩段代碼相同時,可以變成一個函數和兩次調用,相同的部分就是函
數,不同的部分就是調用參數。


2.函數介面的簡潔性:

不是編寫了函數就萬事大吉了,不好的函數介面依然不簡潔。下面是一個剛改好的
例子:

@Html.ImageLink("燃燒圖", true,
"../../Resouces/Images/Agile/CurrentSprint/BurndownData16.png", "icons",
false, "/Agile/CurrentSprint/BurndownData", null, null)

@Html.ImageLink("反覆項目計劃", true,
"../../Resouces/Images/Agile/Sprints/Index16.png", "icons", false,
"/Agile/Sprints/Index", null, null)

...
修改後的結果(另外一段代碼):

@Html.ImageLink("反覆項目計劃", "/Agile/Sprints/Index", true) //這個true有些
地方不太一樣所以留下了。
@Html.ImageLink("迭代故事", "/Agile/Sprints/SprintStories", true)
這種技法:如果函數在重複調用時有些參數變化,有些參數基本不變("icons",
true, false, null....),後者應該變成一些選擇性參數並設定預設值。

新手C#代碼簡潔性—函數篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.