what's the rust consensus on early returns
-
-
Replying to @mountain_ghosts
Use ‘em
2 replies 0 retweets 10 likes -
Replying to @steveklabnik
I assume one-lining `if condition { return; }` is unidiomatic? most of my ruby/js early returns are one-liners
3 replies 0 retweets 1 like -
Replying to @mountain_ghosts
Yeah I use it in Ruby all the time. I tend to not use it in Rust only because 99% of the time you have ? for that kind of thing
2 replies 0 retweets 5 likes -
Replying to @steveklabnik @mountain_ghosts
It's also much more rare in Rust to have a function with those sort of guard clauses and no return value. Presumably it's only being called for side effects, which (in my experience) often means at least returning a `Result`. `if !condition { return Err(ConditionNotMet); }`
1 reply 0 retweets 1 like -
Replying to @sgrif @steveklabnik
I do a fair amount of `return expr if condition` in ruby too -- would one-lining `if condition { return expr }` be considered bad style?
2 replies 0 retweets 0 likes -
Replying to @mountain_ghosts @steveklabnik
Personally I thing "the function will return early if this condition is met" is something that deserves to stand out so I'd want the long form if in both languages.
1 reply 0 retweets 1 like
Lately my early return values have tended to be pretty long as well https://github.com/sgrif/swirl/blob/de5d8bbcfbf7878aa2c248c105938567b32ed8a8/swirl_proc_macro/src/background_job.rs#L79-L115 …
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.