logoalt Hacker News

adamrt06/03/20255 repliesview on HN

Any time I write "if err == nil" I write // inverted just to make it stick out. It would be nice if it was handled by the language but just wanted to share a way to at least make it a bit more visible.

    if err == nil { // inverted
        return err
    }

Replies

hnlmorg06/03/2025

I do something similar. I leave a comment but with a short comment why it’s inverted.

It’s usually pretty obvious why: eg

    if err == nil { 
         // we can exit early because we don’t need to keep retrying
But it at least saves me having to double check the logic of the code each time I reread the code for the first time in a while.
macintux06/03/2025

I know diddly/squat about Go, but from similar patterns in aeons past, would "nil == err" work as a way to make it stand out?

show 2 replies
umanwizard06/05/2025

Something slightly more elegant (in my subjective opinion) you could do is write

  if !(err != nil) {
vhcr06/03/2025

Would be nice if code editors colored it differently so it's easier to see.

prerok06/03/2025

return nil

would be clearer, I think. Seems like it's the same but would color differently in my editor.