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

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)
}
主站蜘蛛池模板: 云梦县| 新晃| 建始县| 当阳市| 苏尼特右旗| 梨树县| 泊头市| 大埔县| 宣汉县| 安图县| 乃东县| 安平县| 阿拉善左旗| 新邵县| 老河口市| 天柱县| 曲麻莱县| 白山市| 壶关县| 福安市| 鹤岗市| 灌云县| 汶上县| 阿合奇县| 景东| 玛曲县| 富川| 宜兰市| 哈密市| 张家界市| 桃源县| 玉林市| 香格里拉县| 桐梓县| 扶沟县| 东明县| 朔州市| 宝鸡市| 濉溪县| 青州市| 尖扎县|