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;
}

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.