The dangers of fatal logging
log.Fatal violates the Single Responsibility Principle in insidious ways. Never use it!I was recently reviewing some code written in Go, where I saw this pattern in a constructor function:
func NewConnection(url string) *Client {
conn, err := service.Connect(url)
if err != nil {
log.Fatal(err)
}
return &Client{conn: conn}
}
The use of log.Fatal
here is a big red flag. In fact, I think that the existence of log.Fatal
, and its equivalent in other languages, is a big misfeature. It violates the Single-Responsibility Principle, which leads to many nasty charactaristics, and even overt bugs. It should be burned!
I’ve written up a longer explanation of my thoughts on the topic for anyone interested.