An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
June 12, 2019 10:35 am GMT
Original Link: https://dev.to/flrnprz/how-do-you-refactor-this-piece-of-code-5eg6
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:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To