偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

如何應(yīng)對不斷膨脹的接口

開發(fā) 前端
難怪碼農(nóng)自嘲是 CRUD boy, 每天確實(shí)在不斷的堆屎,在別人的屎山上縫縫補(bǔ)補(bǔ)。下面的案例并沒有 blame 任何人的意思,我也是堆屎工^^ 如有雷同,請勿對號入座。

 [[442573]]

本文轉(zhuǎn)載自微信公眾號「董澤潤的技術(shù)筆記」,作者董澤潤。轉(zhuǎn)載本文請聯(lián)系董澤潤的技術(shù)筆記公眾號。

難怪碼農(nóng)自嘲是 CRUD boy, 每天確實(shí)在不斷的堆屎,在別人的屎山上縫縫補(bǔ)補(bǔ)。下面的案例并沒有 blame 任何人的意思,我也是堆屎工^^ 如有雷同,請勿對號入座

案例

最近讀一個業(yè)務(wù)代碼,狀態(tài)機(jī)接口定義有 40 個函數(shù),查看 commit log, 初始只有 10 個,每當(dāng)增加新的業(yè)務(wù)需求時,就不斷的在原接口添加

  1. // OrderManager handles operation on order entity 
  2. type OrderManager interface { 
  3.  LoadOrdersByIDs(ctx context.Context, orderIDs []string) ([]*dbentity.Order, error) 
  4.   ...... 
  5.   TransitOrdersToState(ctx context.Context, orderIDs []string, toState orderstate.OrderState) ([]*dbentity.Order, error) 
  6.   ...... 
  7.  Stop() error 

業(yè)務(wù)中很多 interface 都是用來 mock UT, 實(shí)現(xiàn)依賴反轉(zhuǎn),而不是業(yè)務(wù)多態(tài)。OrderManager 就屬于這類,所以接口膨脹后對工程質(zhì)量影響并不大,就是看著不內(nèi)聚...

接口為什么要小

The bigger the interface, the weaker the abstraction.

Go Proverbs[1] Rob Pike 提到:接口越大,抽像能力越弱,比如系統(tǒng)庫中的 io.Reader, io.Writer 等等接口定義只有一兩個函數(shù)。為什么說接口要小呢?舉個例子

  1. type FooBeeper interface { 
  2.   Bar(s string) (string, error) 
  3.   Beep(s string) (string, error) 
  4.  
  5. type thing struct{} 
  6.  
  7. func (l *thing) Bar(s string) (string, error) { 
  8.   ... 
  9.  
  10. func (l *thing) Beep(s string) (string, error) { 
  11.   ... 
  12.  
  13. type differentThing struct{} 
  14.  
  15. func (l *differentThing) Bar(s string) (string, error) { 
  16.   ... 
  17.  
  18. type anotherThing struct{} 
  19.  
  20. func (l *anotherThing) Beep(s string) (string, error) { 
  21.   ... 

接口 FooBeeper 定義有兩個函數(shù): Bar, Beep. 由于接口實(shí)現(xiàn)是隱式的,我們有如下結(jié)論:

  • thing 實(shí)現(xiàn)了 FooBeeper 接口
  • differentThing 沒有實(shí)現(xiàn),缺少 Bar 函數(shù)
  • anotherThing 同樣沒有實(shí)現(xiàn),缺少 Beep 函數(shù)

但是如果我們把 FooBeeper 打散也多個接口的組合

  1. type FooBeeper interface { 
  2.  Bar 
  3.  Beep 
  4.  
  5. type Bar interface { 
  6.  Bar(s string) (string, error) 
  7.  
  8. type Beep interface { 
  9.  Beep(s string) (string, error) 

如上述定義,就可以將接口做小,使得 differentThing anotherThing 可以復(fù)用接口

組合改造

關(guān)于如何改造 OrderManger 可以借鑒 etcd client v3[2] 定義的思想,將相關(guān)的功能聚合成小接口,通過接口的組合實(shí)現(xiàn)

  1. type Client struct { 
  2.  Cluster 
  3.  KV 
  4.  Lease 
  5.  Watcher 
  6.  Auth 
  7.  Maintenance 
  8.  
  9.  conn *grpc.ClientConn 
  10.  
  11.  cfg      Config 
  12.   ...... 

上面是 clientV3 結(jié)構(gòu)體定義,雖然不是接口,但是思想可以借鑒

  1. // OrderManager handles operation on order entity 
  2. type OrderManager interface { 
  3.  OrderOperator 
  4.  TransitOrders 
  5.  Stop() error 

實(shí)際上可能接口只需抽成三個,OrderOperator 負(fù)責(zé)對 orders 的 CRUD 操作,TransitOrders 負(fù)責(zé)轉(zhuǎn)態(tài)機(jī)流轉(zhuǎn),原來的 40 個函數(shù)函數(shù)都放到小接口里面

冗余改造

只抽成小接口是不行的,LoadOrderByXXXX 有一堆定義,根據(jù)不同條件獲取訂單,但實(shí)際上這些都是可以轉(zhuǎn)換的

  1. func LoadOrders(ctx context.Context, FiltersParams options...) 

針對這種情況可以傳入 option, 或是用結(jié)構(gòu)體當(dāng)成參數(shù)容器。再比如狀態(tài)機(jī)流轉(zhuǎn)有 TransitOrdersToState, TransitOrdersToStateByEntity, TransitOrdersStateByEntityForRegularDelivery 均屬于冗余定義

還有一種冗余接口是根本沒人用,或是不該這層暴露的

預(yù)防

接口拆分本質(zhì)上是 ISP Interface segregation principle[3], 不應(yīng)該強(qiáng)迫任何代碼依賴它不使用的方法。IT 行業(yè)有一個笑話

當(dāng)你的 MR 只有幾行時,peer 會提出幾十個 comment. 但是當(dāng)你的 MR 幾百行時,他們只會回復(fù) LGTM

Peer review 還是要有責(zé)任心的,如果成本不高,建議順手把老代碼重構(gòu)一下。重構(gòu)代碼有幾項(xiàng)原則,可以參考 重構(gòu)最佳實(shí)踐2

CI lint 不知道是否支持檢查 interface 行數(shù),但是如果行數(shù)成為指標(biāo),可能又本末倒置了

參考資料

[1]Go Proverbs: https://go-proverbs.github.io/,

[2]etcd client v3: https://github.com/etcd-io/etcd/blob/main/client/v3/client.go#L44,

[3]Interface segregation principle: https://en.wikipedia.org/wiki/Interface_segregation_principle,

 

 

 

責(zé)任編輯:武曉燕 來源: 董澤潤的技術(shù)筆記
相關(guān)推薦

2023-11-14 16:43:17

云計(jì)算數(shù)據(jù)中心

2023-11-09 11:48:41

2021-09-03 15:03:33

數(shù)據(jù)安全安全風(fēng)險(xiǎn)網(wǎng)絡(luò)安全

2015-05-08 12:41:40

2024-10-31 09:51:28

2022-08-01 12:03:43

首席信息官CIOIT

2024-05-06 08:43:00

2025-02-26 07:20:07

2019-11-13 13:37:28

安全內(nèi)部威脅數(shù)據(jù)

2022-07-31 00:10:14

數(shù)據(jù)安全工具數(shù)據(jù)泄露

2020-08-04 10:49:26

云遷移云計(jì)算云平臺

2012-11-14 16:27:41

2022-05-24 10:19:15

網(wǎng)絡(luò)中斷網(wǎng)絡(luò)

2009-09-24 16:56:12

2021-09-07 12:17:58

網(wǎng)絡(luò)攻擊漏洞網(wǎng)絡(luò)安全

2025-07-02 07:25:00

大型語言模型LLM人工智能

2020-01-06 22:56:39

物聯(lián)網(wǎng)安全可穿戴設(shè)備

2013-04-12 09:55:17

遠(yuǎn)程網(wǎng)絡(luò)訪問VPN虛擬化網(wǎng)絡(luò)

2022-07-13 16:54:48

邊緣計(jì)算物聯(lián)網(wǎng)大數(shù)據(jù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號