iT邦幫忙

2025 iThome 鐵人賽

DAY 13
0
Software Development

沒測試也敢重構?IDE 安全重構 30 日生存指南系列 第 17

Day 17. if 的世界線之 4:富有既視感的條件式

  • 分享至 

  • xImage
  •  

昨天提到消除冗余條件的原理,實際應用時會有一點變化,今天就來看看吧。

1. 已經回傳的重複條件式

在這個例子中,order == null 已經在第一個 if 判斷過,並在符合條件時 Early Return 出去了。因此 Rider 判斷在第二個 if 時,永遠不會成立。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414AhjyQVI1AX.png

按下 Show Context Actions,選擇 Remove Expression。其實我一直覺得這個訊息不夠明確,沒辦法清除表達「執行後語意不變」的概念,也許改叫 Remove Redundant Expression 會更好。
https://ithelp.ithome.com.tw/upload/images/20251004/201694148dDvjH0YJ5.png

完成~
https://ithelp.ithome.com.tw/upload/images/20251004/20169414tZ0ggI5DAD.png

2. 巢狀的重複條件式

在這個例子中,明明內外兩個條件式看起來一樣,但 Rider 並沒有像前一次提示我任何訊息。難道是我看錯了嗎?不知道,我不太相信我的肉眼。看看能不能用其他重構方式來幫我釐清這件事。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414181PSesf1u.png

我選擇 Introduce Variable,試著請 Rider 幫我將條件式抽取成變數。
https://ithelp.ithome.com.tw/upload/images/20251004/201694141QQUIBBIGC.png

Rider 問我:「你要提取的是金色的兩個還是銀色的一個呢?」看來我前面判斷得沒錯,這兩個條件是一樣的,幫我把他們抽成同一個變數吧。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414Bd8QjeMQWC.png

當條件式變簡單後,Rider 輕易就可以判斷出重複。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414TnVKiO61yq.png

按下 Show Context Actions,選擇 Replace With 'then result' Expression。這邊即使選擇第二個選項,最終也能達成一樣的結果,只是要花比較多步驟,所以我還是選擇第一個。
https://ithelp.ithome.com.tw/upload/images/20251004/201694147F3LwESQfl.png

成功移除了重複的條件式,但是那個暫時變數看起來有點醜,我打算把它消除掉。
https://ithelp.ithome.com.tw/upload/images/20251004/201694141G6Eqi6Lid.png

執行 Inline Variable
https://ithelp.ithome.com.tw/upload/images/20251004/20169414nC1LD4WDBF.png

完成!
https://ithelp.ithome.com.tw/upload/images/20251004/20169414rE35EnIMGn.png

3. Déjà Vu 的重複條件式

看到這個標題腦中瞬間浮現出頭文字 D 的主題曲 XD。

看看這個例子,單純從字串比對上,這裡並沒有任何的重複,Rider 也沒有任何提示。但第一個 if 的 order.Items.Length <= 0 和第二個 if 的 order.Items.Length > 0 看起來超有關係的。可是經歷了多次把 if 改壞的慘劇,在沒有測試或工具幫助的情況下,我真的不敢直接修改條件式。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414rQRInRZPDf.png

為了讓兩個條件式長成一樣,我選擇 Replace '&&' With '||',Rider 會幫我用 De Morgan's laws 來將它反轉。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414rgRsDqsGaV.png

Ok,重複出現了。但這對 Rider 來說似乎仍偏複雜,它仍然沒有給我任何提示。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414gcwDIb1Tg5.png

這時若像前面一樣直接抽取變數,會變成像這樣,微微地改變語意。這是 Rider 的限制,來看看有沒有辦法繞開。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414PNEQ4DF7aX.png

我先用 Split 'if' 把它拆開。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414lHKX62KqDs.png

變這樣。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414d2rDuLcD6a.png

接著再執行 Introduce Variable,這邊直接跳到結果。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414509lkCAVPw.png

可以看到變成簡單條件式後,Rider 終於給出了提示。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414DbX7Y4eFy2.png

接著執行 Remove Expression
https://ithelp.ithome.com.tw/upload/images/20251004/20169414LOAEOtThnT.png

再執行 Remove Redundant Parentheses
https://ithelp.ithome.com.tw/upload/images/20251004/20169414v6npEqjlTX.png

已經很接近了。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414RtSCn6RMfF.png

執行 Inline Variable 後,就算是告一個段落了。步驟有點多,但最終還是成功移除了重複的條件式。
https://ithelp.ithome.com.tw/upload/images/20251004/20169414YTnJNzOlG3.png

小結

要判斷冗余條件式,並不是一個很簡單的事情,即使是 Rider 也有它的極限在,但我們仍能透過很多不同的方式來跟 Rider 互動,進而成功執行任務。值得一提的是,所有的操作從頭到尾都沒有打過一行 code。這就是自動重構工具的魅力!


上一篇
Day 16. if 的世界線之 3:斬斷冗余的枷鎖
系列文
沒測試也敢重構?IDE 安全重構 30 日生存指南17
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言