成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何重構一些可怕的代碼

開發 前端
重構本身并不總是合理的。 但是,很難想象有人會抱怨增加測試范圍,尤其是在涵蓋如此關鍵的功能的情況下。

最近,我不得不重構一些處于粗糙狀態的代碼:

  1. function endpoint(service, version = '') { 
  2.   let protocol 
  3.   let domain 
  4.  
  5.   if (service === 'webclient') { 
  6.     protocol = __CLIENT_PROTOCOL__ 
  7.     domain = `${__ENV__}-${service}.${__DOMAIN__}` 
  8.     if (__SERVER__) { 
  9.       protocol = 'http' 
  10.     } else if (__ENV__ === 'production') { 
  11.       domain = `www.${__DOMAIN__}` 
  12.     } 
  13.   } else { 
  14.     if (__ENV__ !== 'local') { 
  15.       if (__SERVER__) { 
  16.         protocol = 'http' 
  17.         domain = `${__ENV__}-${service}` 
  18.         domain += `.${__DOMAIN__}` 
  19.       } else { 
  20.         protocol = __CLIENT_PROTOCOL__ 
  21.         domain = `${__ENV__}-api.${__DOMAIN__}` 
  22.         if (service !== 'core') { 
  23.           domain += `/${service}` 
  24.         } 
  25.         if (version) { 
  26.           domain += `/${version}` 
  27.         } 
  28.       } 
  29.     } else { 
  30.       protocol = 'http' 
  31.  
  32.       if (service === 'core') { 
  33.         if (__CLIENT__) { 
  34.           domain = `api.${__DOMAIN__}` 
  35.         } else { 
  36.           domain = `api.${__DOMAIN__}:80` 
  37.         } 
  38.       } else { 
  39.         if (__CLIENT__) { 
  40.           domain = `api.${__DOMAIN__}/${service}/${version}` 
  41.         } else { 
  42.           domain = `api.${__DOMAIN__}:80/${service}/${version}` 
  43.         } 
  44.       } 
  45.     } 
  46.   } 
  47.  
  48.   const url = `${protocol}://${domain}` 
  49.  
  50.   return url 
  51.  
  52. export default endpoint 

上面的邏輯確定端點的URL,它取決于多種因素:您使用的服務,在服務器或客戶端上呈現的服務,在生產環境或測試環境中使用的服務等。

一段代碼變得如此混亂的原因之一是,當我們忘記重復比錯誤的抽象要便宜得多時。

好消息是,您可以應用幾種簡單的技術來簡化嵌套的if-else語句。 壞消息是,這段代碼對應用程序至關重要,因為所有請求都在調用它。 哦,它也沒有經過測試!

所以,這就是我重構的方式…

1. 給該代碼100%的測試覆蓋率

我沒有執行重構代碼的任務,所以打電話了。 但是我不知道要花多少時間(我本該用來交付其他東西的時間)。

重構本身并不總是合理的。 但是,很難想象有人會抱怨增加測試范圍,尤其是在涵蓋如此關鍵的功能的情況下。 因此,我決定從此開始,不僅是為了讓我對重構充滿信心,而且因為當我完成測試時,我將更好地了解重構的難度,并且我可以進行/不進行。 去打電話。

如今,我們大多數人都在Gousto中實踐TDD,但是代碼庫的這一特定部分是幾年前的,它的重要性和狀態阻礙了我和其他人在過去對其進行重構。

TDD的主要好處是無需擔心,也無需花費成本即可進行重構-羅伯特·馬丁(Robert C. Martin,鮑勃叔叔)

我還想確保覆蓋率是100%,所以我使用了Jest標志–coverage,它提供了以下輸出:

  1. -------------------|----------|----------|----------|----------|-------------------| 
  2. File               |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s | 
  3. -------------------|----------|----------|----------|----------|-------------------| 
  4.   ...              |      ... |      ... |      ... |      ... |                ...| 
  5.   endpoint.js      |      100 |      100 |      100 |      100 |                   | 
  6.   ...              |      ... |      ... |      ... |      ... |                ...| 
  7. -------------------|----------|----------|----------|----------|-------------------| 
  8. Test Suites: 1 passed, 1 total 
  9. Tests:       12 passed, 12 total 

2. 提取功能

現在我們有了測試,有了更多的信心,我們可以開始拆分代碼。 讓我們從頭開始。 我們看到,根據服務,環境,客戶端/服務器端……為協議分配了不同的值,然后將其添加到函數末尾的其余url中。

  1. const url = `${protocol}://${domain}` 

因此,我們可以將確定協議的代碼抽象為自己的函數,只需調用一次即可:

  1. import endpoint from 'config/endpoint' 
  2.  
  3. describe('endpoint.js', () => { 
  4.   global.__DOMAIN__ = 'gousto.local' 
  5.   let service 
  6.  
  7.   describe('when the service is "webclient"', () => { 
  8.     beforeEach(() => { 
  9.       service = 'webclient' 
  10.     }) 
  11.  
  12.     describe('and being in the server side', () => { 
  13.       beforeEach(() => { 
  14.         global.__SERVER__ = true 
  15.         global.__ENV__ = 'whateverenv' 
  16.       }) 
  17.  
  18.       test('an http address with the corresponding ENV, SERVICE and DOMAIN is returned', () => { 
  19.         const url = endpoint(service) 
  20.         expect(url).toBe(`http://${__ENV__}-${service}.${__DOMAIN__}`) 
  21.       }) 
  22.     }) 
  23.  
  24.     describe('and not being in the server side', () => { 
  25.       ... 
  26.       describe('and the environment is production', () => { 
  27.         ... 
  28.         test('an https address with "www" and without the service, but with the DOMAIN is returned', () => {...}) 
  29.       }) 
  30.  
  31.       describe('and the environment is not production', () => { 
  32.         ... 
  33.         test('an https address with the corresponding ENV, SERVICE and DOMAIN is returned', () => {...}) 
  34.       }) 
  35.     }) 
  36.   }) 
  37.  
  38.   describe('when the service is not "webclient"', () => { 
  39.     ... 
  40.     describe('and the env is not "local"', () => { 
  41.       ... 
  42.       describe('and being in the server side', () => { 
  43.         ... 
  44.         test('an http address with the corresponding ENV, SERVICE and DOMAIN is returned', () => {...}) 
  45.       }) 
  46.  
  47.       describe('and not being in the server side', () => { 
  48.         ... 
  49.         describe('and the service is core', () => { 
  50.           ... 
  51.           test('an https API address with the corresponding ENV and DOMAIN is returned', () => {...}) 
  52.  
  53.           describe('and a version was passed', () => { 
  54.             test('an https API address with the corresponding ENV, DOMAIN, SERVICE and VERSION is returned', () => {...}) 
  55.           }) 
  56.         }) 
  57.  
  58.         describe('and a version was passed', () => { 
  59.           test('an https API address with the corresponding ENV, DOMAIN, SERVICE and VERSION is returned', () => {...}) 
  60.         }) 
  61.  
  62.         describe('and a version was not passed', () => { 
  63.           test('an https API address with the corresponding ENV, DOMAIN and SERVICE is returned', () => {...}) 
  64.         }) 
  65.       }) 
  66.     }) 
  67.  
  68.     describe('and the env is "local"', () => { 
  69.       ... 
  70.       describe('and the service is core', () => { 
  71.         ... 
  72.         describe('and being in the client side', () => { 
  73.           ... 
  74.           test('an http API address with the corresponding DOMAIN is returned', () => {...}) 
  75.         }) 
  76.  
  77.         describe('and not being in the client side', () => { 
  78.           ... 
  79.           test('an http API address with the corresponding DOMAIN and port 80 is returned', () => {...}) 
  80.         }) 
  81.       }) 
  82.  
  83.       describe('and the service is not core', () => { 
  84.         ... 
  85.         describe('and being in the client side', () => { 
  86.           ... 
  87.           test('an http API address with the corresponding DOMAIN, SERVICE and VERSION is returned', () => {...}) 
  88.         }) 
  89.  
  90.         describe('and not being in the client side', () => { 
  91.           ... 
  92.           test('an http API address with the corresponding DOMAIN, port 80, SERVICE and VERSION is returned', () => {...}) 
  93.         }) 
  94.       }) 
  95.     }) 
  96.   }) 
  97. }) 

我們可以對URL的其余部分執行與getProtocol()相同的操作。 提取的功能越多,if-else的地獄就越容易簡化,提取剩余的內容就越容易。 因此,我的建議是從最小的復雜度入手,因為這將使其余的操作更為簡單。

提取這些函數并不復雜,但這是因為我將if-else的噩夢翻譯成了它們。 因此,我們沒有大的混亂,但有一些小混亂。 不能接受 讓我們來照顧它。

3. 簡化

除了關注點分離之外,將url的不同部分提取為不同功能的優點是可以進一步簡化條件。 之前,URL的某些部分限制了簡化,因為它們需要滿足多個條件。 現在,它們是分開的,所以我們不必為此擔心。

您可以只是出于對它的關注而進行簡化……或者您可以使用真值表來提供幫助。

對于getProtocol(),一種真值表如下所示:

但可以簡化一下("-"表示該值無關緊要):

我們只有兩個可能的值:http和https,因此我們可以采用較少行(https)的值,檢查條件,然后返回另一個(http)。

  1. const getProtocol = (service, isServerSide, environment) => { 
  2.   if (service === 'webclient' && !isServerSide) { 
  3.     return 'https' 
  4.   } 
  5.    
  6.   if (service !== 'webclient' && !isServerSide && environment !== 'local') { 
  7.    return 'https'  
  8.   } 
  9.    
  10.   return 'http' 

這已經比上一節中的要好。 但是我們仍然可以做更多! 檢查前兩個條件,您可能會發現只有在我們不在服務器端時才獲得https。 因此,我們可以為http編寫第一個條件,并在函數的其余部分中擺脫isServerSide:

  1. const getProtocol = (service, isServerSide, environment) => { 
  2.   if (isServerSide) { 
  3.     return 'http' 
  4.   } 
  5.    
  6.   if (service === 'webclient') { 
  7.     return 'https' 
  8.   } 
  9.    
  10.   if (service !== 'webclient' && environment !== 'local') { 
  11.    return 'https'  
  12.   } 
  13.    
  14.   return 'http' 

不過,我們可以做得更好。 現在我們可以合并第二個和第三個條件,因為它們較小:

  1. const getProtocol = (service, isServerSide, environment) => { 
  2.   ... 
  3.   if (service === 'webclient' ||  
  4.       (service !== 'webclient' && environment !== 'local')) { 
  5.     return 'https' 
  6.   } 
  7.   ... 

但是,請保持……這種狀況有點愚蠢,不是嗎? 如果服務是webclient,則條件為true。 否則……轉到OR的第二部分,為什么我們需要檢查服務是否不是webclient? 如果我們在OR的右側,那么我們確定它不是webclient。 最終結果看起來很整潔,特別是與第2部分中的原始結果相比。提取功能:

  1. const getProtocol = (service, isServerSide, environment) => { 
  2.   if (isServerSide) { 
  3.     return 'http' 
  4.   } 
  5.  
  6.   if (service === 'webclient' || environment !== 'local') { 
  7.     return 'https' 
  8.   } 
  9.  
  10.   return 'http' 

結果

多虧了功能的提取,我們最終有了一個endpoint()函數,這是我寫的榮幸。

  1. ... 
  2.  
  3. function endpoint(service, version = '') { 
  4.   const protocol = getProtocol(service, __SERVER__, __ENV__) 
  5.   const subdomain = getSubdomain(service, __SERVER__, __ENV__) 
  6.   const path = getPath(service, __SERVER__, __ENV__, version) 
  7.   const port = getPort(service, __ENV__, __CLIENT__) 
  8.  
  9.   return `${protocol}://${subdomain}.${__DOMAIN__}${port}${path}` 
  10.  
  11. export default endpoint 

上面代碼的get函數很小,足以被理解而不會動您的大腦。 并非所有人都像我想要的那樣簡單,但是它們比原始代碼要好得多。 您可以在這里看到文件的外觀。

最后,您應該始終嘗試遵循以下規則:讓代碼比發現的要好。 盡管贊美❤️不要這么做

> GitHub comment in the PR

> A team mate making my day

現在輪到您了,您是否有任何想法使它變得更簡單(無需更改在其余應用程序中的使用方式)? 您對重構的處理方式會有所不同嗎?

 

責任編輯:趙寧寧 來源: 今日頭條
相關推薦

2020-09-21 06:58:56

TS 代碼建議

2009-11-26 10:32:57

PHP代碼優化

2009-06-22 15:36:00

如何學好java

2012-12-24 14:51:02

iOS

2021-10-31 07:36:17

前端JavaScript編程

2019-08-01 12:56:00

Python代碼記錄

2023-03-09 17:54:04

2013-03-29 09:03:59

iOS實用小代碼iOS開發

2009-08-04 16:06:19

ASP.NET代碼分離

2009-06-18 14:54:52

Spring AOP

2009-09-21 17:46:25

Hibernate數據

2011-06-01 16:50:21

JAVA

2013-07-02 09:43:02

編程策略

2013-07-02 10:18:20

編程編程策略

2016-11-16 21:18:42

android日志

2009-06-25 09:50:32

JSF

2020-02-03 16:03:36

疫情思考

2010-09-28 14:14:19

SQL語句

2013-03-29 13:17:53

XCode調試技巧iOS開發

2011-07-13 09:13:56

Android設計
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产一区二区三区视频 | 黄色网址在线播放 | 免费高清av| 午夜伦理影院 | 成人久久久久 | 青青草原精品99久久精品66 | 国产小视频在线 | 日本久久视频 | 日本免费一区二区三区四区 | 亚洲午夜小视频 | 国产日产久久高清欧美一区 | 综合网中文字幕 | 国产精品一区三区 | 日韩精品免费播放 | 亚洲第一中文字幕 | 在线观看国产视频 | 国产成人精品综合 | 日韩在线| 美女在线观看av | 欧美成人精品欧美一级 | 久久久国产精品一区 | 久久精品亚洲精品国产欧美 | 国产精品18hdxxxⅹ在线 | 欧美精品成人一区二区三区四区 | 一区二区三区电影网 | 草久视频| 欧美二区在线 | 欧美精品福利视频 | 五月天国产视频 | 精品亚洲永久免费精品 | 韩日在线视频 | 澳门永久av免费网站 | 国产精品片 | 久久久国产一区二区三区四区小说 | 一本一道久久a久久精品蜜桃 | 亚洲精品在线看 | 国产在视频一区二区三区吞精 | 91视视频在线观看入口直接观看 | 成人国产精品久久久 | 国产一区二区精品在线 | 日韩三级免费观看 |