開心一笑
【某大公司老板巡視倉庫時,發(fā)現(xiàn)一位工人坐在地上看漫畫痰哨。
老板最恨工人在工作時間偷懶胶果,便生氣地問:“你一個月賺多少?”
工人:“一千斤斧≡缈伲”
老板立刻叫旁邊的職員給他一千塊,并大叫:“你給我滾撬讽!”
事后蕊连,老板便問職員:“那工人是誰介紹的?”
職員說:“他不是本公司的人游昼,是其他公司派來送貨的甘苍。”
這下更氣了烘豌≡赝ィ】
提出問題
項目開發(fā)中如何簡化條件表達式?廊佩?囚聚?
解決問題
下面來自《重構(gòu)》這本書的筆記和自己的一點總結(jié),希望可以節(jié)省大家看書時間标锄。
Decompose Conditional(分解條件表達式)
你有一個復雜的條件表達式(if-then-else)語句顽铸。從if,then鸯绿。else三個段落中分別提煉出獨立函數(shù)跋破。
例一:
public void test(){
int sendDay = 1000;//堅持送了幾天
int flowerNum = 9;//花朵數(shù)量
int engagementDay = 100000;//約會天數(shù)
int callMinuteTimes = 10000;
//重構(gòu)前
if(engagementDay > 100000 && callMinuteTimes > 10000 && sendDay * flowerNum >999999){
System.out.println("恭喜你" + "--" + "可以結(jié)婚了,姑娘F亢毒返!");
}
//重構(gòu)后,例子比較簡單舷手,沒什么需要解釋
if(isLove(engagementDay,callMinuteTimes,sendDay,flowerNum)){
System.out.println("恭喜你" + "--" + "可以結(jié)婚了拧簸,姑娘!男窟!");
}
}
public boolean isLove(int engagementDay,int callMinuteTimes,int sendDay,int flowerNum){
return engagementDay > 100000 && callMinuteTimes > 10000 && sendDay * flowerNum >999999;
}
Consilidate Conditional Expression(合并條件表達式)
你有一系列條件測試盆赤,都得到相同結(jié)果。將這些測試合并為一個條件表達式歉眷。并將這個條件表達式提煉成為一個獨立函數(shù)牺六。
例二:
@Test
public boolean test(){
int sendDay = 1000;//堅持送了幾天
int flowerNum = 9;//花朵數(shù)量
int engagementDay = 100000;//約會天數(shù)
int callMinuteTimes = 10000;
//重構(gòu)前
if(engagementDay > 100000) return true;
if(callMinuteTimes > 10000) return true;
if (sendDay * flowerNum >999999) return true;
//重構(gòu)后
if(isLove(engagementDay,callMinuteTimes,sendDay,flowerNum)){
return true;
}
return false;
}
public boolean isLove(int engagementDay,int callMinuteTimes,int sendDay,int flowerNum){
return engagementDay > 100000 || callMinuteTimes > 10000 || sendDay * flowerNum >999999;
}
Consolidate Duplicate Conditional Fragments(合并重復的條件片段)
在條件表達式的每個分支上,有著相同的一段代碼汗捡,將這段重復代碼搬移到條件表達式之外淑际。
例三:
//重構(gòu)前
if(isSpecialDeal()){
total = price * 0.95;
send();
}else{
total = price * 0.98;
send();
}
//重構(gòu)后
if(isSpecialDeal()){
total = price * 0.95;
}else{
total = price * 0.98;
}
send();
Remove Control Flag(移除控制標記)
在一系列布爾表達式中,某個變量帶有控制標記的作用,以break語句或return語句取代控制標記春缕。
例四:
重構(gòu)前:
@Test
public boolean test(int loveCallNum){
//某個變量帶有控制標記的作用
boolean isLove = false;
for(int i=0;i<loveCallNum;i++){
if(isLove){
System.out.println("我們結(jié)婚吧盗胀!");
}else{
if(i == 999){
isLove = true;
}
}
}
}
重構(gòu)后:
for(int i=0;i<loveCallNum;i++){
if(i == 999){
System.out.println("我們結(jié)婚吧!");
//以break語句或return語句取代控制標記锄贼。
break;
}
}
Replace Nested Conditional with Guard Clauses(以衛(wèi)語句取代嵌套條件表達式)
函數(shù)中的條件邏輯使人難以看清正常的執(zhí)行路徑票灰。使用衛(wèi)語句表現(xiàn)所有特殊情況。
衛(wèi)語句:就是把復雜的條件表達式拆分成多個條件表達式宅荤,比如一個很復雜的表達式屑迂,嵌套了好幾層的if-then-else語句,轉(zhuǎn)換為多個if語句膘侮,實現(xiàn)它的邏輯屈糊,這多條的if語句就是衛(wèi)語句
例五:
重構(gòu)前:
int sendDay = 1000;//堅持送了幾天
int flowerNum = 9;//花朵數(shù)量
int engagementDay = 100000;//約會天數(shù)
int callMinuteTimes = 10000;
//重構(gòu)前
if(engagementDay < 100000){
System.out.println("待定......");
}else{
if(callMinuteTimes < 10000){
System.out.println("待定......");
}else{
if(sendDay * flowerNum >999999){
System.out.println("真愛......");
}
}
}
重構(gòu)后:下面就是衛(wèi)語句
if(engagementDay < 100000) System.out.println("待定......");
if(callMinuteTimes < 10000) System.out.println("待定......");
if(sendDay * flowerNum >999999) System.out.println("真愛......");
Replace Conditional with Polymorphism(以多態(tài)取代條件表達式)
你手上有個條件表達式,他根據(jù)對象類型的不同而選擇不同的行為琼了,將這個條件表達式的每個分支放進一個子類內(nèi)的覆寫函數(shù)中逻锐,然后將原始函數(shù)聲明為抽象函數(shù)。
Introduce Assertion(引入斷言)
某一段代碼需要對程序狀態(tài)作出某種假設(shè)雕薪,以斷言明確表現(xiàn)這種假設(shè)昧诱。
例六:
//獲取子任務(wù)對象
PcsSubTask pcsSubTask = pcsSubTaskService.findById(subTaskId);
//在這里添加斷言,確定pcsSubTask確實不為空
AssertUtils.checkResourceFound(pcsSubTask);
讀書感悟
來自莫泊伤《項鏈》
- 極細小的一件事可以成全你盏档,也可以敗壞你。
- 倘若當時沒有失掉那件首飾燥爷,她現(xiàn)在會走到什么樣的境界蜈亩?誰知道?誰知道前翎?人生真是古怪稚配,真是變化無常啊。無論是害您或者救您港华,只消一點點小事道川。
- 浪費了?噢立宜,不冒萄!你去上班的時候,我常常坐在窗邊想橙数,如果沒有弄丟那條項鏈尊流,我會是什么樣子?現(xiàn)在灯帮,我知道答案了奠旺。
其他
如果有帶給你一絲絲小快樂蜘澜,就讓快樂繼續(xù)傳遞下去,歡迎轉(zhuǎn)載响疚,點贊,頂瞪醋,歡迎留下寶貴的意見忿晕,多謝支持!