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

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)
}
主站蜘蛛池模板: 垫江县| 绥滨县| 沂南县| 上饶市| 望奎县| 乌鲁木齐市| 威海市| 西安市| 通许县| 万安县| 静乐县| 班戈县| 临高县| 梅河口市| 涡阳县| 伊金霍洛旗| 屏东市| 肥西县| 桂林市| 英吉沙县| 南阳市| 田东县| 当雄县| 黄平县| 昌乐县| 黄平县| 台东县| 西宁市| 新巴尔虎右旗| 西昌市| 新竹市| 崇明县| 松江区| 武义县| 淮南市| 繁昌县| 呼和浩特市| 桦川县| 上虞市| 扎兰屯市| 乾安县|