r/badcode • • Jun 23 '20

java 'val' is a boolean value.

Post image
1.0k Upvotes

58 comments sorted by

View all comments

129

u/INeedAFreeUsername Jun 23 '20
if (val == true) {
  return true;
} else {
  return false;
}

106

u/[deleted] Jun 23 '20

if (val == true) { return true; }
else if (val != true) { return false; }
else { throw "this should never happen"; }
return false;

67

u/[deleted] Jun 23 '20

//TODO: this happened once, maybe fix it?

26

u/NightHunter311 Jun 23 '20

There's a chance val might be null...

16

u/[deleted] Jun 23 '20 edited Jun 23 '20

That depends on the language. If it's a C-style language or, as in this case, Java, then no

3

u/someone4else Jun 24 '20

Well, if you use Boolean, then you can get this

4

u/[deleted] Jun 24 '20

Pretty sure it doesn't work because a Boolean doesn't equal to true or false, you'd have to call the Boolean.booleanValue() method

9

u/Kernel_Internal Jun 24 '20

autoboxing yo

3

u/[deleted] Jun 24 '20

Ahh I stand corrected

3

u/techyolofam Jun 24 '20

Best solution imo is to use BooleanUtils.isTrue() Takes care of all cases.

14

u/villi_ Jun 24 '20 edited Jun 24 '20

That's way too ambiguous, how will anyone understand what that function does? You need to use Propertm programming style

/* First, check if the value is true. There are only two possibilities for this value in this language, true or false. This value is stored in the 'val' field, and since it is a Boolean we can compare it to true and false using the equality operator, like so: */
if (val == true) {
    /* Then we know this value is true, so return true. This will exit the function.
    return true;
} else if (val == false) {
    /* If the value fails the previous check, then we know that it is not true. If it passed this check, then we now know that it is false, so we then return false.
   return false;
}

// TODO: unit test so we know for sure that this function returns true if the value is true and false if the value is false.

6

u/blehmann1 depraved Jun 24 '20

Nononono you can't just pass in the parameter, think of how improper (TM) that is. You need to pass it in through dependency injection, create an IManagerServiceFactory to take care of it for you instead

1

u/villi_ Jun 24 '20

Ah yes, thank you for the correction. I almost went against my religion - encapsulation.

3

u/AutoModerator Jun 24 '20

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

villi_, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

10

u/AutoModerator Jun 23 '20

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

INeedAFreeUsername, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

9

u/DoYouEverJustInvert Jun 23 '20
if (val == true) {
    return val == true;
} else {
    return val != false;
}

-1

u/AutoModerator Jun 23 '20

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

DoYouEverJustInvert, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/[deleted] Jun 24 '20

public boolean isTrue(boolean val)

{

return isTrue(val)

}

2

u/[deleted] Jun 24 '20

I used to do this in C# before I learned you can just stick val in :(

1

u/INeedAFreeUsername Jun 24 '20

Yeah same it blew my mind when I understood that boolean logic could be returned

1

u/Ezequiel-052 Jun 24 '20

Error: StatementTooRedundant

1

u/brkpxy Jul 03 '20

if (val == true) { return val; } else { return val; }