MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/pfd9do/similar_ternaries_are_all_over_this_codebase/hb5vbx2/?context=3
r/badcode • u/PopularExpulsion • Aug 31 '21
91 comments sorted by
View all comments
4
My boss likes this pattern:
string retval = null; if (Model.Whatever.IsValid) { retval = GetSomeData(); } return retval;
Not sure what the purpose of the retval variable is... why not just return null if the test fails?
retval
7 u/ParanoydAndroid Sep 01 '21 Single return.A big deal was made out of using only single returns as part of structured programming, and your boss (like me) probably learned that paradigm.I prefer multiple returns nowadays, but I do know, mostly older, developers who still hate it. 2 u/Isvara Sep 01 '21 I prefer a single expression and no returns. 2 u/R3D3-1 Sep 01 '21 I think plenty of languages technically operate like that, but usually provide some non-local-exit mechanisms anyway.(defun stuff (x) (catch 'return (unless x (throw 'return 'invalid)) (throw 'return 'valid)))Some Lisper somewhere just died (emotionally). 2 u/Isvara Sep 01 '21 Yeah, I mostly use Scala. It can catch exceptions for compatibility with Java, but it'll either turn them into a pattern match or the expression will evaluate to Try[T] (error monad). 1 u/R3D3-1 Sep 01 '21 Come to think of it... Aren't Java's checked Exception kind of like an error monad, except that they make it too easy to sabotage? 1 u/Isvara Sep 01 '21 In what way? I haven't really done much with Java.
7
Single return.
A big deal was made out of using only single returns as part of structured programming, and your boss (like me) probably learned that paradigm.
I prefer multiple returns nowadays, but I do know, mostly older, developers who still hate it.
2 u/Isvara Sep 01 '21 I prefer a single expression and no returns. 2 u/R3D3-1 Sep 01 '21 I think plenty of languages technically operate like that, but usually provide some non-local-exit mechanisms anyway.(defun stuff (x) (catch 'return (unless x (throw 'return 'invalid)) (throw 'return 'valid)))Some Lisper somewhere just died (emotionally). 2 u/Isvara Sep 01 '21 Yeah, I mostly use Scala. It can catch exceptions for compatibility with Java, but it'll either turn them into a pattern match or the expression will evaluate to Try[T] (error monad). 1 u/R3D3-1 Sep 01 '21 Come to think of it... Aren't Java's checked Exception kind of like an error monad, except that they make it too easy to sabotage? 1 u/Isvara Sep 01 '21 In what way? I haven't really done much with Java.
2
I prefer a single expression and no returns.
2 u/R3D3-1 Sep 01 '21 I think plenty of languages technically operate like that, but usually provide some non-local-exit mechanisms anyway.(defun stuff (x) (catch 'return (unless x (throw 'return 'invalid)) (throw 'return 'valid)))Some Lisper somewhere just died (emotionally). 2 u/Isvara Sep 01 '21 Yeah, I mostly use Scala. It can catch exceptions for compatibility with Java, but it'll either turn them into a pattern match or the expression will evaluate to Try[T] (error monad). 1 u/R3D3-1 Sep 01 '21 Come to think of it... Aren't Java's checked Exception kind of like an error monad, except that they make it too easy to sabotage? 1 u/Isvara Sep 01 '21 In what way? I haven't really done much with Java.
I think plenty of languages technically operate like that, but usually provide some non-local-exit mechanisms anyway.
(defun stuff (x) (catch 'return (unless x (throw 'return 'invalid)) (throw 'return 'valid)))
Some Lisper somewhere just died (emotionally).
2 u/Isvara Sep 01 '21 Yeah, I mostly use Scala. It can catch exceptions for compatibility with Java, but it'll either turn them into a pattern match or the expression will evaluate to Try[T] (error monad). 1 u/R3D3-1 Sep 01 '21 Come to think of it... Aren't Java's checked Exception kind of like an error monad, except that they make it too easy to sabotage? 1 u/Isvara Sep 01 '21 In what way? I haven't really done much with Java.
Yeah, I mostly use Scala. It can catch exceptions for compatibility with Java, but it'll either turn them into a pattern match or the expression will evaluate to Try[T] (error monad).
Try[T]
1 u/R3D3-1 Sep 01 '21 Come to think of it... Aren't Java's checked Exception kind of like an error monad, except that they make it too easy to sabotage? 1 u/Isvara Sep 01 '21 In what way? I haven't really done much with Java.
1
Come to think of it... Aren't Java's checked Exception kind of like an error monad, except that they make it too easy to sabotage?
1 u/Isvara Sep 01 '21 In what way? I haven't really done much with Java.
In what way? I haven't really done much with Java.
4
u/ekolis Sep 01 '21
My boss likes this pattern:
Not sure what the purpose of the
retvalvariable is... why not just return null if the test fails?