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

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)
}
主站蜘蛛池模板: 惠水县| 乐昌市| 安图县| 林芝县| 鱼台县| 类乌齐县| 克东县| 中西区| 得荣县| 通河县| 门源| 巴南区| 封开县| 常宁市| 安西县| 湘阴县| 上栗县| 抚顺市| 凤山市| 商河县| 涞源县| 贡嘎县| 渝中区| 望江县| 房产| 社旗县| 江川县| 鸡东县| 靖江市| 宜兴市| 华阴市| 湖州市| 九江县| 汉源县| 化隆| 孝昌县| 万山特区| 新营市| 侯马市| 九江市| 沾益县|