logoalt Hacker News

flakesyesterday at 7:35 AM1 replyview on HN

I'd say a lot of users are going to borrow patterns from Go, where you'd typically check the error first.

    resource, err := newResource()
    if err != nil {
        return err
    }
    defer resource.Close()
IMO this pattern makes more sense, as calling exit behavior in most cases won't make sense unless you have acquired the resource in the first place.

free may accept a NULL pointer, but it also doesn't need to be called with one either.


Replies

maccardyesterday at 8:40 AM

This example is exactly why RAII is the solution to this problem and not defer.

show 2 replies