3 ms·
This looks neat, but I would not call it clear. In cases where I need to check many conditions and return, I would simply use a conditional switch (and also use
by ainar-g 8y ago
This looks neat, but I would not call it clear. In cases where I need to check many conditions and return, I would simply use a conditional switch (and also use exact error values instead of generated ones):
switch {
case transfer == nil:
return nil, ErrInvalidTransfer
case person == nil:
return nil, ErrInvalidPerson
// ...
}
This, to me, is both concise and clear.
- laumars 8y agoI do the same. I can see the appeal of wanting to create a package to manage error checking when you have dozens of conditions but this doesn't bring anything new to the table yet creates code that is arguably less maintainable and certainly less easily read.
- storozhukBM 8y agoYou can actually return specific errors using verifier library. Check out documentation: https://godoc.org/github.com/storozhukBM/verifier#Verify.WithError https://godoc.org/github.com/storozhukBM/verifier#Verify.Wit...
- nostalgeek 8y agoI believe the point of the OP is, you don't need the verifier library at first place. This is one of these dependencies that offer very little for the cost of adding a new dependency to a project.
- icholy 8y agoI'm not going to use OP's package, but I'll definitely copy the idea into a local helper. https://gist.github.com/icholy/fc41b8cfe74506cf93ec6b0fba1fc4ce https://gist.github.com/icholy/fc41b8cfe74506cf93ec6b0fba1fc...
- Groxx 8y agoSmall benefit to Verifier in this case: it can capture multiple errors. A switch can only capture one.