開心一笑
【某大公司老板巡視倉庫時,發現一位工人坐在地上看漫畫。
老板最恨工人在工作時間偷懶,便生氣地問:“你一個月賺多少?”
工人:“一千。”
老板立刻叫旁邊的職員給他一千塊,并大叫:“你給我滾!”
事后,老板便問職員:“那工人是誰介紹的?”
職員說:“他不是本公司的人,是其他公司派來送貨的。”
這下更氣了。】
提出問題
項目開發中如何簡化條件表達式???
解決問題
下面來自《重構》這本書的筆記和自己的一點總結,希望可以節省大家看書時間。
Decompose Conditional(分解條件表達式)
你有一個復雜的條件表達式(if-then-else)語句。從if,then。else三個段落中分別提煉出獨立函數。
例一:
public void test(){
int sendDay = 1000;//堅持送了幾天
int flowerNum = 9;//花朵數量
int engagementDay = 100000;//約會天數
int callMinuteTimes = 10000;
//重構前
if(engagementDay > 100000 && callMinuteTimes > 10000 && sendDay * flowerNum >999999){
System.out.println("恭喜你" + "--" + "可以結婚了,姑娘!!");
}
//重構后,例子比較簡單,沒什么需要解釋
if(isLove(engagementDay,callMinuteTimes,sendDay,flowerNum)){
System.out.println("恭喜你" + "--" + "可以結婚了,姑娘!!");
}
}
public boolean isLove(int engagementDay,int callMinuteTimes,int sendDay,int flowerNum){
return engagementDay > 100000 && callMinuteTimes > 10000 && sendDay * flowerNum >999999;
}
Consilidate Conditional Expression(合并條件表達式)
你有一系列條件測試,都得到相同結果。將這些測試合并為一個條件表達式。并將這個條件表達式提煉成為一個獨立函數。
例二:
@Test
public boolean test(){
int sendDay = 1000;//堅持送了幾天
int flowerNum = 9;//花朵數量
int engagementDay = 100000;//約會天數
int callMinuteTimes = 10000;
//重構前
if(engagementDay > 100000) return true;
if(callMinuteTimes > 10000) return true;
if (sendDay * flowerNum >999999) return true;
//重構后
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(合并重復的條件片段)
在條件表達式的每個分支上,有著相同的一段代碼,將這段重復代碼搬移到條件表達式之外。
例三:
//重構前
if(isSpecialDeal()){
total = price * 0.95;
send();
}else{
total = price * 0.98;
send();
}
//重構后
if(isSpecialDeal()){
total = price * 0.95;
}else{
total = price * 0.98;
}
send();
Remove Control Flag(移除控制標記)
在一系列布爾表達式中,某個變量帶有控制標記的作用,以break語句或return語句取代控制標記。
例四:
重構前:
@Test
public boolean test(int loveCallNum){
//某個變量帶有控制標記的作用
boolean isLove = false;
for(int i=0;i<loveCallNum;i++){
if(isLove){
System.out.println("我們結婚吧!");
}else{
if(i == 999){
isLove = true;
}
}
}
}
重構后:
for(int i=0;i<loveCallNum;i++){
if(i == 999){
System.out.println("我們結婚吧!");
//以break語句或return語句取代控制標記。
break;
}
}
Replace Nested Conditional with Guard Clauses(以衛語句取代嵌套條件表達式)
函數中的條件邏輯使人難以看清正常的執行路徑。使用衛語句表現所有特殊情況。
衛語句:就是把復雜的條件表達式拆分成多個條件表達式,比如一個很復雜的表達式,嵌套了好幾層的if-then-else語句,轉換為多個if語句,實現它的邏輯,這多條的if語句就是衛語句
例五:
重構前:
int sendDay = 1000;//堅持送了幾天
int flowerNum = 9;//花朵數量
int engagementDay = 100000;//約會天數
int callMinuteTimes = 10000;
//重構前
if(engagementDay < 100000){
System.out.println("待定......");
}else{
if(callMinuteTimes < 10000){
System.out.println("待定......");
}else{
if(sendDay * flowerNum >999999){
System.out.println("真愛......");
}
}
}
重構后:下面就是衛語句
if(engagementDay < 100000) System.out.println("待定......");
if(callMinuteTimes < 10000) System.out.println("待定......");
if(sendDay * flowerNum >999999) System.out.println("真愛......");
Replace Conditional with Polymorphism(以多態取代條件表達式)
你手上有個條件表達式,他根據對象類型的不同而選擇不同的行為,將這個條件表達式的每個分支放進一個子類內的覆寫函數中,然后將原始函數聲明為抽象函數。
Introduce Assertion(引入斷言)
某一段代碼需要對程序狀態作出某種假設,以斷言明確表現這種假設。
例六:
//獲取子任務對象
PcsSubTask pcsSubTask = pcsSubTaskService.findById(subTaskId);
//在這里添加斷言,確定pcsSubTask確實不為空
AssertUtils.checkResourceFound(pcsSubTask);
讀書感悟
來自莫泊桑《項鏈》
- 極細小的一件事可以成全你,也可以敗壞你。
- 倘若當時沒有失掉那件首飾,她現在會走到什么樣的境界?誰知道?誰知道?人生真是古怪,真是變化無常啊。無論是害您或者救您,只消一點點小事。
- 浪費了?噢,不!你去上班的時候,我常常坐在窗邊想,如果沒有弄丟那條項鏈,我會是什么樣子?現在,我知道答案了。
其他
如果有帶給你一絲絲小快樂,就讓快樂繼續傳遞下去,歡迎轉載,點贊,頂,歡迎留下寶貴的意見,多謝支持!