Your Web News in One Place
June 12, 2019 10:35 am GMT

How do you refactor this piece of code?

Hello fellow gophers, I need some insights. I'm writing a command line client for larder.io bookmark in go. I have this function:

func checkConfigRoute(configDir string) (string, error) {    usr, err := user.Current()    if err != nil {        return nil, err    }    route := join(usr.HomeDir, "/", configDir)    // Check if the directory exist, if not, create it the route.    if _, err := os.Stat(route); os.IsNotExist(err) {        if err := os.MkdirAll(route, os.ModePerm); err != nil {            return nil, err        }    }    return route, nil}

I need to check if a config file exist, but before that I want to check if the route directory exist, if not create it and return the route.

I don't know why but it feels off, not sure if I'm doing it wrong. What do you think? How do you refactor it? Should I pass the full route directly to the function instead of joining it inside? Help much appreciated!

Thanks!


Original Link: https://dev.to/flrnprz/how-do-you-refactor-this-piece-of-code-5eg6

Share this article:  Share on Twitter  Share on Facebook
View Full Article

Dev.To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev.To