gcache icon indicating copy to clipboard operation
gcache copied to clipboard

Panics should never happen in a library

Open pcman312 opened this issue 8 years ago • 0 comments

Panicking in a library is an anti-pattern in Go. It results in indeterministic and unpredictable code. The caller has no idea when a library is going to panic unless they are experts in the library's codebase.

Currently, this is being done in several places: https://github.com/bluele/gcache/blob/bbe6d2a3c95ca6a646e924974dad20247fd38496/cache.go#L70 https://github.com/bluele/gcache/blob/bbe6d2a3c95ca6a646e924974dad20247fd38496/cache.go#L163

I believe a better option would be to allow for any size in the New() function, but then checking it in the Build() and returning an error. This makes it more idiomatic and removes the possibility that the caller's program crashes unexpectedly because of a bad input.

pcman312 avatar Jul 17 '17 16:07 pcman312