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

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)
}
主站蜘蛛池模板: 潼关县| 岳西县| 六枝特区| 莫力| 五家渠市| 拜城县| 资兴市| 新绛县| 灵石县| 浏阳市| 邯郸市| 色达县| 吉安县| 呼和浩特市| 南皮县| 济宁市| 宝鸡市| 定南县| 望谟县| 藁城市| 抚州市| 弋阳县| 五寨县| 喀喇沁旗| 富顺县| 河津市| 宣恩县| 巴南区| 潞城市| 抚顺市| 淮南市| 许昌县| 远安县| 宣恩县| 临邑县| 琼海市| 武穴市| 柞水县| 南陵县| 东丽区| 乐东|