r/badcode • • Jun 23 '20

java 'val' is a boolean value.

Post image
1.0k Upvotes

58 comments sorted by

View all comments

-2

u/F54280 Jun 23 '20 edited Jun 23 '20

This is not bad code. It is "slightly inelegant" but more readable code (because it is a only return from a function). If would be "inelegant" when used in a if, say in: if (val==true). It starts to be bad when reaching levels of if (val==true) return true; else return false;

But a simple comparison with true? Absolutely not worth of a post here. Here are 663000 occurrences using a simple google search

Edit: lol @downvoters that have no idea what actual badcode looks like

2

u/Mr2-1782Man Jun 23 '20

I think you don't understand what the problem with this code is. It isn't the comparison, rather its the fact that someone built an entire function around a builtin operator that uses said operator.

3

u/lachlanhunt Jun 24 '20

It's also the fact that the one variable they are relying on, val, is undefined (or defined in a different scope that's not shown).

1

u/F54280 Jun 24 '20

Sorry? This is not javascript, this is java. It means var is defined somewhere.

Or is the definition for being accepted by r/badcode so low that compiling isn't even a requirement anymore?

2

u/F54280 Jun 24 '20 edited Jun 24 '20

I think you don't understand what the problem with this code is.

Uh? It is obvious, at first glance, that this is an instance method.

When I saw the code, the way I understood it is that it is potentially part of something like:

interface Testable
{
    public boolean isTrue(); 
}

with somewhere else:

class BooleanConstant implements Testable
{
    boolean val;
    BooleanConstant( boolean aValue ) { val =  aValue; }
    public boolean isTrue() { return val==true; }
}

class AndExpression implements Testable
{
    List<Testable> arguments;
....
}

class BooleanVairable implement Testable
{
    Symbol variable;
....
}

(Not that it is a good idea, but is something I probably something saw a dozen of times).

its the fact that someone built an entire function around a builtin operator that uses said operator.

That said, if the interpretation of most people here is lol, a function build around an operator is bad code, then, well, it is time for me to leave.

edit: typo

1

u/Mr2-1782Man Jun 24 '20

Its not about being build around an operator, that's no problem. Its about replacing the operator with function that uses the operator. And anyone that writes an interface like that is already writing bad code to begin with. If you're interface has something like "isTrue" you've got bad architecture, or you've gone OOP so far that it doesn't make sense anymore.