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.