- 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)
}
推薦閱讀
- 程序員面試白皮書
- Drupal 8 Blueprints
- Android Studio Essentials
- Mastering LibGDX Game Development
- 軟件測試技術(shù)指南
- Django Design Patterns and Best Practices
- 貫通Tomcat開發(fā)
- CodeIgniter Web Application Blueprints
- JavaScript悟道
- 軟件工程與UML案例解析(第三版)
- 從零開始學(xué)Unity游戲開發(fā):場景+角色+腳本+交互+體驗+效果+發(fā)布
- Mastering XenApp?
- Developer,Advocate!
- R語言數(shù)據(jù)分析從入門到實戰(zhàn)
- Python 3.8編程快速入門