商業需求對於Refactor的重要性

  1. 易閱讀
  2. 易維護
  3. 易測試
  1. 一次只能放一個廣告的位置需要做檢查,其他一次可以投放多個廣告的位置不需要檢查
  2. 當新增一個廣告,需要檢查已經存在的廣告是否有佔用想要投放新廣告的位置和時段
  3. 和已存在廣告的時段、country, language條件都有交集表示此狀況下不能再新增廣告
  4. 當要更新已經存在的廣告則需要從GetExistAds排除掉目前要更新的廣告, 否則永遠都會比對到自己
If( new/updated Ad's position only can put an Ad )
{
if( the period time that the new/updated Ad display is not available &&
the exist Ad's position only can put an Ad &&
the exist Ad isn't the new/updated Ad )
{
if( new/updated Ad's country has intersection with exist Ad's country &&
new/updated Ad's language has intersection with exist Ad's language )
{
The position isn't available
}
}
}
原始程式碼

從商業需求、目的角度思考

If ( the position is empty )
{
put other Ad
}
Refactor結果

Refactor使用的相關技巧

  1. 語法糖的使用
Extension method
Restrictions是一個List<object>
Restriction object裡的條件也是List
OnlySupportOneAd推入Position物件
Position Object
OnlySupportOneAd推入Position model後的使用方式

總結

  1. Refactor前或實作feature前先理解method或feature的目的及價值
  2. 了解目前要做的東西本質是什麼才選擇要使用哪種語法糖
  3. 決定參數要放什麼object之前多想想有哪些好處
  4. 將method推進model之前考慮和model是否高內聚

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Michelle Hung

Michelle Hung

More from Medium

Why Do You Give A Gift? The Top 5 Reasons Why Gift Giving Is Important

Break the bias

The Best Korean Supermarket in Dubai