官术网_书友最值得收藏!

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)
}
主站蜘蛛池模板: 左云县| 曲水县| 甘孜县| 杭锦后旗| 当雄县| 扬中市| 延川县| 霍州市| 蒙城县| 东海县| 襄樊市| 贵阳市| 科技| 宝清县| 广昌县| 达孜县| 巨鹿县| 临汾市| 客服| 城步| 六盘水市| 马山县| 汤原县| 什邡市| 德庆县| 内乡县| 武平县| 黑河市| 永平县| 思南县| 茂名市| 宁安市| 丹棱县| 永定县| 柞水县| 安塞县| 乐业县| 京山县| 上饶市| 安达市| 大同市|