DEV Community

Discussion on: [Go] unusual: move "if err !=nil" into struct methods

Collapse
 
andreidascalu profile image
Andrei Dascalu

Well, that's not quite so. If it were only the if checks, it wouldn't be much overhead if you don't overdo it, but we're also talking about calling functions unnecessarily. When you call a function, there are all sorts of allocations happening and garbage collection. There's an overhead to function calls beyond whether they run their full code or not. You should run a profiler test.

There's another bit. When checking on demand, you may have a reusable err variable that only ever holds nil in happy case an at most one error which is collected at the end of function call.

I'm you case you have a full error allocated regardless of usage. Even in the happy case you have an error that's only garbage collected when the whole program ends.

Of course, the last point is also a nitpick in that it's not idiomatic.