掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流
看著自己每次根據(jù)設計原則及模式的代碼重構,雖效果不錯,但也自省:如果我的每段代碼都這么寫,是不是過度設計?把握設計的度,需長久錘煉。行業(yè)也總結了很多原則,幫助我們把握設計的度。它們是一種思考方法、一種行為準則。

創(chuàng)新互聯(lián)公司專注于沙雅網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供沙雅營銷型網(wǎng)站建設,沙雅網(wǎng)站制作、沙雅網(wǎng)頁設計、沙雅網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務,打造沙雅網(wǎng)絡公司原創(chuàng)品牌,更為您提供沙雅網(wǎng)站排名全網(wǎng)營銷落地服務。
Keep it simple, stupid,保持簡單、愚蠢。提醒我們大多數(shù)系統(tǒng),與其變得復雜,保持簡單能讓系統(tǒng)運行更好。越資深的人,越覺得這大有道理。因為大佬們見識過因為復雜而引發(fā)的各種問題。堆太多功能,調(diào)整起來就很費勁:
真是吸引crud boy,但無法指導具體工作。啥叫保持簡單,怎么就叫復雜?這都沒標準。有人基于自己的理解給具體原則:
You aren’t gonna need it,你用不著它。如非必要,勿增功能。軟件設計對抗的是需求規(guī)模:
很多需求不需要做。很多產(chǎn)品經(jīng)理以為很重要的功能實際上是沒什么用的。真正重要的功能大約只占20%。做了更多的功能,并不會得到更多的回報,但是,做了更多的功能,軟件本身卻會不斷地膨脹,越難維護。
所以,在現(xiàn)實經(jīng)??吹揭恍┕δ芎唵蔚臇|西不斷涌現(xiàn),去顛覆更復雜東西。如Word強大,但只是個寫字工具,重點排版功能都用得少。而Markdown簡單地讓我們專注寫內(nèi)容,而且簡單的幾個排版標記在日常溝通中就完全夠用。盡量可能不去做不該做的事,從源頭堵住問題。
Don’t repeat yourself,不要重復自己。在一個系統(tǒng)中,每一處知識都必須有單一、明確、權威地表述。Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
即不要做cv工程師。這還遠遠不夠,DRY針對的是你對知識和意圖的復制:在兩個不同地方的兩樣東西表達形式不同,但表達內(nèi)容卻可能相同。如下打印賬戶信息:
public void printBalance(final Account account) {
System.out.printf("Debits: %10.2f\n", account.getDebits());
System.out.printf("Credits: %10.2f\n", account.getCredits());
if (account.getFees() < 0) {
System.out.printf("Fees: %10.2f-\n", -account.getFees());
} else {
System.out.printf("Fees: %10.2f\n", account.getFees());
}
System.out.printf(" ----\n");
if (account.getBalance() < 0) {
System.out.printf("Balance: %10.2f-\n", -account.getBalance());
} else {
System.out.printf("Balance: %10.2f\n", account.getBalance());
}
}這段隱藏一些重復。如對負數(shù)的處理顯然是復制的,可通過增加一個方法消除:
String formatValue(final double value) {
String result = String.format("%10.2f", Math.abs(value));
if (value < 0) {
return result + "-";
} else {
return result + " ";
}
}
void printBalance(final Account account) {
System.out.printf("Debits: %10.2f\n", account.getDebits());
System.out.printf("Credits: %10.2f\n", account.getCredits());
System.out.printf("Fees:%s\n", formatValue(account.getFees()));
System.out.printf(" ----\n");
System.out.printf("Balance:%s\n", formatValue(account.getBalance()));
}數(shù)字字段格式反復出現(xiàn),不過,格式與我們抽取出來的方法一致,復用:
String formatValue(final double value) {
String result = String.format("%10.2f", Math.abs(value));
if (value < 0) {
return result + "-";
} else {
return result + " ";
}
}
void printBalance(final Account account) {
System.out.printf("Debits: %s\n", formatValue(account.getDebits()));
System.out.printf("Credits: %s\n", formatValue(account.getCredits()));
System.out.printf("Fees:%s\n", formatValue(account.getFees()));
System.out.printf(" ----\n");
System.out.printf("Balance:%s\n", formatValue(account.getBalance()));
}打印格式其實也重復,如果我要在標簽和金額之間加一個空格,相關的代碼都要改,所以,這也是一個可以消除的重復:
String formatValue(final double value) {
String result = String.format("%10.2f", Math.abs(value));
if (value < 0) {
return result + "-";
} else {
return result + " ";
}
}
void printLine(final String label, final String value) {
System.out.printf("%-9s%s\n", label, value);
}
void reportLine(final String label, final double value) {
printLine(label + ":", formatValue(value));
}
void printBalance(final Account account) {
reportLine("Debits", account.getDebits());
reportLine("Credits", account.getCredits());
reportLine("Fees", account.getFees());
System.out.printf(" ----\n");
reportLine("Balance", account.getBalance());
}重構后:
有人說這種調(diào)整粒度太小。如你這樣感覺,證明你看問題的粒度太大。品味這個修改,與分離關注點和單一職責原則異曲同工:粒度要小。
DRY不局限于寫代碼:
都是在試圖減少重復,其實也是減少了維護成本。
Simple Design,提出者Kent Beck,只包含如下規(guī)則,后3條規(guī)則是重構方向
保證系統(tǒng)能按預期工作。怎么知道系統(tǒng)按照預期工作,就需要有配套自動化測試,最好能TDD,最根本的還是要懂設計,否則,你的代碼就是不可測。
正如DRY,你得能發(fā)現(xiàn)重復,就要會分離關注點
編寫有表達性的代碼,這也需要你對“什么是有表達性的代碼”有認識。代碼要說明做什么,而不是怎么做
讓類和方法的數(shù)量最小化,不要過度設計,除非你已看到這必須要做個設計,比如,留下適當擴展點,否則,就不要做。能做出過度設計的前提,是已懂得各種設計,這時才需要用簡單設計的標準對自己約束。所謂簡單設計,對大多數(shù)人并不“簡單”。
沒有良好設計,代碼就沒有可測試的接口,TDD就無從談起。不懂設計,重構就只是簡單提取方法,改改名字,對代碼的改進相當有限。
簡單設計的前提是,把編程基礎打牢。片面地追求敏捷實踐,而忽視基本功,是舍本逐末。
網(wǎng)頁標題:代碼過度設計,真的有意義嗎?
鏈接分享:http://uogjgqi.cn/article/cosdhcs.html

我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流