iT邦幫忙

2025 iThome 鐵人賽

DAY 12
0
Software Development

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

Day 12. Change Signature 之 2:讓參數垂直移動

  • 分享至 

  • xImage
  •  

昨天提到 Change Signature 可以調整參數的順序,我們可以把這種重構視為參數的「水平移動」。若我們想要把某些參數的處理移出或移入方法/函式內,也就是參數的「垂直移動」,則可以用今天介紹的技巧:Introduce ParameterInline Parameter

1. Introduce Parameter 將參數往上移動

延續昨天的例子。Post 方法當中有個:

var url = baseUrl + action

我覺得這個組裝的邏輯不應該出現在 Post 當中,而是應該由呼叫方自行處理,Post 只需要接收完整的 url 就好。
https://ithelp.ithome.com.tw/upload/images/20250926/20169414uI5kauJdpk.png

把游標移到我希望移動的變數上,也就是 url,選擇 Introduce Parameter
https://ithelp.ithome.com.tw/upload/images/20250926/20169414Ji4335KShb.png

這時彈窗顯示我要抽出的參數名稱,可以注意到下面提示我原本的 baseUrl 和 action,在把 url 往上提出後便不會被用到,因此可以一起刪掉。前一天提到實測 Rider 在處理多步驟的操作時,有機率把簽章改壞,所以這邊我刻意不勾選。直接按 Next。
https://ithelp.ithome.com.tw/upload/images/20250926/20169414oJIg3iGXOO.png

可以看到 url 的組裝邏輯被搬到呼叫方了。不過目前還有一點醜,我們繼續往下。
https://ithelp.ithome.com.tw/upload/images/20250926/20169414Q9T1nWO3qU.png

選取其中一個灰色的參數,按下習慣的 Show Context Actions,Rider 說可以移除這個沒用到的參數,太好了!按下去~
https://ithelp.ithome.com.tw/upload/images/20250926/201694143kfd1q0r06.png

雖然畫面上看不出來,但 Rider 可是一口氣幫我把 20 個呼叫方都更新了!另一個灰色的參數我用 Safe Delete 來完成。
https://ithelp.ithome.com.tw/upload/images/20250926/20169414jSl0hTpgWg.png

這樣變乾淨多了。
https://ithelp.ithome.com.tw/upload/images/20250926/20169414ZAYLf4yyBu.png

最後做一些小調整,我把游標移到 url 後面的 + 號上,按 Show Context Actions,調整一下字串組裝的寫法。
https://ithelp.ithome.com.tw/upload/images/20250926/20169414CiwL2KmU5I.png

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

最後再用 Change Signature 調整一下順序,過程就跳過了,最終結果如下。
https://ithelp.ithome.com.tw/upload/images/20250926/20169414vKQ7Aovg6h.png

2. Inline Parameter 將參數往下移動

才一會的功夫,同事也學會了 Introduce Parameter,趁我去上廁所的時間,悄悄地把 content type 也抽成參數。
https://ithelp.ithome.com.tw/upload/images/20250926/20169414LPE8aDn22b.png

我耐心地跟他解釋,我們所有的 API 都用 "application/json",不需要特意把它抽成參數。
https://ithelp.ithome.com.tw/upload/images/20250926/20169414xFcDiHhrWT.png

於是我在這個字串上按下 Inline Parameter
https://ithelp.ithome.com.tw/upload/images/20250926/20169414T28FG50rCN.png

1 秒把它變回了原本的樣子。
https://ithelp.ithome.com.tw/upload/images/20250926/20169414vcZcn6h44m.png

3. 注意事項

這裡有件特別要注意的事,Inline Parameter 時,Rider 只會選擇其中一個呼叫方的值代入。以這個例子來說,若真的有地方代入 "application/x-www-form-urlencoded",這個重構就會壞掉。所以我通常會在執行前用 Show Value Origin 檢查呼叫方的值。
https://ithelp.ithome.com.tw/upload/images/20250927/20169414VConWXKaOo.png

Rider 把所有呼叫方代入的值都列出來了,一眼就可以檢查完畢,確實都是 "application/json"。
https://ithelp.ithome.com.tw/upload/images/20250927/20169414mNHY4PZUmm.png

若是你擔心可能會有微小的差異,讓肉眼誤判,以這個例子來說,可以給參數提供預設值。這時 Rider 會提示你這個參數可以從呼叫方移除。
https://ithelp.ithome.com.tw/upload/images/20250927/20169414MIZ8IiiNat.png

全部執行完後,用 Show Value Origin 確認所有的呼叫方都是使用預設值,再執行 Inline Parameter
https://ithelp.ithome.com.tw/upload/images/20250927/20169414F5fSMgiBwG.png

小結

雖然調整參數的步驟看似很多,實際上執行大概也就是不到 15 秒的時間。感謝 Rider 又讓我度過平安的一天。


上一篇
Day 11. Change Signature:改寫方法的契約
下一篇
Day 13. Change Signature 之 3:老闆幫我把這邊的參數打包一下
系列文
沒測試也敢重構?IDE 安全重構 30 日生存指南14
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言