- 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)
}
推薦閱讀
- Vue 3移動Web開發與性能調優實戰
- Functional Python Programming
- Mastering Visual Studio 2017
- 大學計算機基礎實驗教程
- 跟“龍哥”學C語言編程
- Python從入門到精通(精粹版)
- C語言程序設計
- Learning ELK Stack
- Windows Forensics Cookbook
- Learning Python Design Patterns
- Building RESTful Python Web Services
- 深入分布式緩存:從原理到實踐
- Regression Analysis with Python
- 機器學習微積分一本通(Python版)
- Hadoop大數據分析技術