- Hands-On Dependency Injection in Go
- Corey Scott
- 135字
- 2021-06-10 19:17:50
Apply just enough abstraction
Excessive abstraction leads to an excessive mental burden and excessive typing. While some may argue that any code fragment that could be swapped out or extended later deserves an abstraction, I would argue for a more pragmatic approach. Implement enough to deliver the business value we are tasked with and then refactor as needed. Look at the following code:
type myGetter interface {
Get(url string) (*http.Response, error)
}
func TooAbstract(getter myGetter, url string) ([]byte, error) {
resp, err := getter.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
Compare the previous code to the following usage of the commonly understood concept:
func CommonConcept(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
推薦閱讀
- C#程序設計教程
- Building Minecraft Server Modifications
- 實戰Java高并發程序設計(第3版)
- 微信小程序入門指南
- R語言與網絡輿情處理
- Internet of Things with ESP8266
- Unity 3D腳本編程:使用C#語言開發跨平臺游戲
- Backbone.js Testing
- Machine Learning for OpenCV
- 奔跑吧 Linux內核
- Linux Networking Cookbook
- Java程序設計(項目教學版)
- Python GUI設計tkinter菜鳥編程(增強版)
- Hands-On Artificial Intelligence with Unreal Engine
- SEO的藝術(原書第2版)