I kinda wish there was a better way to do if err != nil in go (github.com)
from danhab99@programming.dev to golang@programming.dev on 25 Sep 2023 03:20
https://programming.dev/post/3462277

Did I break a taboo by doing this?

#golang

threaded - newest

dbx12@programming.dev on 25 Sep 2023 04:38 next collapse

I too consider that if err != nil a bit complicated to type. Most times, I wrap it away in a function like your Check0. I know that the major “framework” for command line applications (cobra) has a similar logic with its checkError function.

theherk@lemmy.world on 25 Sep 2023 06:09 next collapse

I don’t think it is a taboo, but it is possibly just worse code in many cases. Handling error values is usually something that should be done thoughtfully. Panic is nice but can easily be overused and should not make it out of a package.

I too wish there was a better way, but in the form of proper enums and result and option types. However, wrapping in functions is fine in some cases not taboo.

As an aside, you probably don’t need a static union type for your min and max functions. I assume you could use comparable.

danhab99@programming.dev on 25 Sep 2023 13:06 collapse

As an aside, you probably don’t need a static union type for your min and max functions. I assume you could use comparable.

comparible only allows ==

theherk@lemmy.world on 25 Sep 2023 16:20 collapse

Oh right! What am I saying? contraints.Ordered maybe.

danhab99@programming.dev on 26 Sep 2023 01:41 collapse

I just looked at the standard docs and I didn’t realize there was a built-in min and max function. Y’all should really read the standard lib docs… it’s fascinating in there.

ck_@discuss.tchncs.de on 25 Sep 2023 06:28 next collapse

I would call it a taboo because it diverges from the general best practice. That makes your code harder to read and understand for people less familiar with “your style”. Given that code is read much more often that it is written, you are optimizing on the wrong end.

otl@lemmy.sdf.org on 25 Sep 2023 09:24 next collapse

There is! See “Errors are values” from the Go blog: go.dev/blog/errors-are-values

danhab99@programming.dev on 26 Sep 2023 01:42 collapse

Yeah I know about that article… Then again… I’m lazy

podatus@programming.dev on 26 Sep 2023 01:41 collapse

I would rename Check to Must which there is at least some precedent for.

danhab99@programming.dev on 26 Sep 2023 01:44 collapse

Oh yeah and it’s also less to type. Good idea!

Edit: here’s the change github.com/danhab99/idk/blob/main/idk.go#L13