- 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)
}
推薦閱讀
- Google Flutter Mobile Development Quick Start Guide
- UML和模式應用(原書第3版)
- 深入淺出Spring Boot 2.x
- FreeSWITCH 1.6 Cookbook
- Java程序員面試算法寶典
- Quarkus實踐指南:構建新一代的Kubernetes原生Java微服務
- 機器學習與R語言實戰
- Visual Basic程序設計
- 零基礎學Kotlin之Android項目開發實戰
- Java 從入門到項目實踐(超值版)
- SQL Server 入門很輕松(微課超值版)
- C編程技巧:117個問題解決方案示例
- 深度學習程序設計實戰
- Visual Basic程序設計基礎
- 零基礎學SQL(升級版)