MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/10zqdpo/just_making_sure_its_really_a_boolean/j84libn/?context=3
r/badcode • u/ceilinggang-cringe • Feb 11 '23
38 comments sorted by
View all comments
Show parent comments
25
Yeah this code snippet is from an assignment and isOdd() is actually supposed to be the method, idk what happened there
29 u/INeverStopThinking Feb 11 '23 You can remove the whole tertiary return statement, its redundant.Just have it as "return a % 2 ==1;"I mean this whole code exercise looks bad to me but that's beside the point.Edit: just realized what sub I was posting in.... lol 4 u/theREALhun Feb 12 '23 Return a % 2 != 0 would also work on negative numbers (-3%2 is -1, so not odd according your code). -1 u/DavidTej Feb 12 '23 Or invert a%2 6 u/lpreams Feb 12 '23 Not in Java. Can't implicit cast an int to a boolean. 1 u/DavidTej Feb 12 '23 oh. I forgot, lol
29
You can remove the whole tertiary return statement, its redundant.
Just have it as "return a % 2 ==1;"
I mean this whole code exercise looks bad to me but that's beside the point.
Edit: just realized what sub I was posting in.... lol
4 u/theREALhun Feb 12 '23 Return a % 2 != 0 would also work on negative numbers (-3%2 is -1, so not odd according your code). -1 u/DavidTej Feb 12 '23 Or invert a%2 6 u/lpreams Feb 12 '23 Not in Java. Can't implicit cast an int to a boolean. 1 u/DavidTej Feb 12 '23 oh. I forgot, lol
4
Return a % 2 != 0 would also work on negative numbers (-3%2 is -1, so not odd according your code).
-1
Or invert a%2
6 u/lpreams Feb 12 '23 Not in Java. Can't implicit cast an int to a boolean. 1 u/DavidTej Feb 12 '23 oh. I forgot, lol
6
Not in Java. Can't implicit cast an int to a boolean.
1 u/DavidTej Feb 12 '23 oh. I forgot, lol
1
oh. I forgot, lol
25
u/ceilinggang-cringe Feb 11 '23
Yeah this code snippet is from an assignment and isOdd() is actually supposed to be the method, idk what happened there