Show HN: Returnif – the conditional return statement
suczewski.com
suczewski.com
This, funny enough, reminds me of Go. We've been having debates about how to improve syntax of `if err != nil { return err }`, and many of us (myself included) disliked helpers to immediately return or hide an error value.
The reason is that `if err != nil { return err }` is actually somewhat non-idiomatic. It returns the error value with no additional context of where the error occurred, what is wrong, etc. So something like `returnif` in Go, I think is bad. For reference, this is a more idiomatic example: `if err != nil { return fmt.Errorf("foo bar: %v",err) }` where `foo bar` adds context about the thing returning the error.
So perhaps my Go logic doesn't apply here in JS land, but it seems like a bad keyword, if it's similar to Go.
So if you wanted to format the error returned you could do `returnif error : formatError("Error context:", error)`
You mentioned that this is a proof of concept hack. What's the plan ahead?
Was mostly a fun project to prove out & build a new idea. No plan ahead for this, but always new projects in the pipeline!
err = checkErr()
if (err)
return err
Vs returnif checkErr()
However, seems a bit overboard to add a reserved keyword for this 'exceptional' case.`returnif` is fun, but there's certainly value in the simplicity of restricted, opinionated syntax (e.g Go).
Pointless sugar
Postconditions ala ruby would be "neat", still kinda pointless tho.