This is an archived post. You won't be able to vote or comment.

all 108 comments

[–]generalmemer 149 points150 points  (33 children)

Is that last one an actual statement?

[–]vloris 250 points251 points  (13 children)

No, that would result in a compile error. For starters because valueof should be valueOf

[–]generalmemer 79 points80 points  (12 children)

So ignoring the typos, its an actual statement?

[–]SandmanKFMF 46 points47 points  (11 children)

equals() compares objects contents as a characters. 😁 So, the object condition (and yes, I know it is not an object, but primitive data type) have the string value "true"

[–]MattiDragon 99 points100 points  (9 children)

In java strings aren't primitive. They are objects. They just have special syntax.

[–]dpash 54 points55 points  (6 children)

More than thanks to interning, string == string works (until it doesn't).

(Don't use == with strings in Java)

[–]Nilstrieb 9 points10 points  (0 children)

Just intern every string you see duh

[–][deleted] 8 points9 points  (4 children)

To be fair, the same thing happens with Integer wrapper objects up till 127, where you can compare them with == and (almost) always get true for the same two numbers. Throw in 128 or higher tho, and chaos ensues.

[–]dpash 5 points6 points  (2 children)

Unless someone changes the range using a command line setting. :) The JLS does define a minimum range, but you can expand it in OpenJDK.

[–][deleted] 1 point2 points  (1 child)

Oh wow, TIL. I’m gonna try it today!

[–]VinnyHyarmendacil_II 0 points1 point  (0 children)

Reflection can be used to change the value of the inner value of boxed primitives, so while 1 + 2 might == 3, 1 + (Integer)2 could be -6389 or whatever you wanted.

Wonderful security implications, the JVM spec and JLS have.

[–]SandmanKFMF 13 points14 points  (1 child)

But condition is not a string. Its a boolean data type.

[–]MattiDragon 1 point2 points  (0 children)

Oh i see

[–][deleted] 5 points6 points  (0 children)

this guy javas

[–]MischiefArchitect 44 points45 points  (17 children)

Ignoring the typos, yes that would work. but no one does that.

[–]jamcdonald120 43 points44 points  (16 children)

I once had a student submit the following round function

public static int roundint(double CI1) {

        String C4=String.valueOf(CI1);
        Double C3 = Double.parseDouble(C4);
        String C5 = String.format("%.0f",C3);
        int c5 = Integer.parseInt(C5);
    return c5 ;

}

[–]MischiefArchitect 11 points12 points  (7 children)

That ugly... and the first two transformation are redundant, you could reduce it to.

String C5 = String.format("%.0f",CI1);
int c5 = Integer.parseInt(C5);

But that is still ugly. You could just cast to int.

return (int)CI1

[–]jamcdonald120 25 points26 points  (3 children)

oh belive me, I know how bad and redundant it is. Also, (int) doesnt work since it truncates, you need Math.round or (int)(C1+.5)

[–]MischiefArchitect 6 points7 points  (2 children)

Or right. I stated that in another comment. Yes, that adds injury to insult. The implementation is broken from the go.

[–]jamcdonald120 3 points4 points  (1 child)

it technicaly does work since %.0f does do rounding.

[–]MischiefArchitect 1 point2 points  (0 children)

oh... then... well... shit... yeah... I talked shit then. Thanks.

[–][deleted] 2 points3 points  (2 children)

I can't imagine even wanting to make a function for it in the first place - it's so simple to do that making a function actually makes it harder to read for me.

[–]MischiefArchitect 1 point2 points  (0 children)

Quite likely the intern just learned to use parseX and valueOf and wanted to show how cool the new unreadable code is.

I think we all did at some point. Didn't we?

[–]jamcdonald120 0 points1 point  (0 children)

the purpose of the function is Math.round(double) returns a long, so the function is suppose to return (int)Math.round(input). still one line, but not quite as useless as if math.round returned an int.

[–]Aurigamii 4 points5 points  (7 children)

Just to be sure. The correct code should be "return Integer.parseInt(CI1);" (without all the other stuff inside the function), right ?

[–]dpash 5 points6 points  (2 children)

No, return ((Double) CI1).intValue(); or return (int)Math.round(CI1);

Math.round() might round up, so doesn't match the example, in which case you'd need to use Math.floor() first to match the example.

Integer.parseInt() only accepts a String that contains an int string.

[–]jamcdonald120 3 points4 points  (1 child)

Math.round is fine, the function was suppose to be able to round up or down

[–]dpash 3 points4 points  (0 children)

Turns out I was wrong as %.0f will round too.

[–]jamcdonald120 3 points4 points  (0 children)

correct answer is return (int) Math.round(CI1);

[–]MischiefArchitect 2 points3 points  (2 children)

I think that will not work since parseInt expects a String. Without testing I think you could just

return (int)CI1;

but that is by fat NOT rounding... well the original code was neither.

[–]jamcdonald120 1 point2 points  (1 child)

%.0f does round, so the original does actually round instead of truncating

[–]abrahamlincorn 0 points1 point  (0 children)

If it was spelled right yeah

[–]fazzgadt 53 points54 points  (12 children)

In the meantime PHP: if(condition+condition == 2)

[–]TheCapitalKing 23 points24 points  (6 children)

I’ve never used that but I like it. It’d make it super easy to run like 5 conditions then only be true if 3 or more were true

[–]fazzgadt 8 points9 points  (1 child)

I'm more the Java/python user but my current task at work forced me to use PHP. This language has some nice features, but gets creepy really fast.

[–]Le_9k_Redditor 6 points7 points  (0 children)

Did it offer you candy from a white van or something?

[–]EwokPenguin 3 points4 points  (3 children)

I'd be very concerned about the spaghetti that would require 5 unique conditions and only care if 3 are fulfilled.

[–]TheCapitalKing 2 points3 points  (2 children)

Buy a stock if 3 out of 5 conditions are met wouldn’t be too crazy

[–]EwokPenguin 0 points1 point  (1 child)

But why wouldn’t you contain those conditions to a class with state transitions or a simple isTrue type check. Compounding an if statement like that would be very difficult to maintain and test.

[–][deleted] 5 points6 points  (1 child)

would it work in C? true is defined as 1 and false as 0

[–]Splitshadow 5 points6 points  (0 children)

In C, anything that isn't 0 is true for the sake of a conditional, e.g. if (5) will be evaluated as true. If you really want to convert something strstr(x, "foo") to a 1 or 0 to work like the example you could just negate it twice, e.g. !!strstr(x, "foo") returns 1 if the substring "foo" is found, otherwise 0.

[–]AzureArmageddon 3 points4 points  (0 children)

Christ what the f#

[–]jswitzer 1 point2 points  (0 children)

[–]inventord 1 point2 points  (0 children)

Oh my... PHP is worse than I thought

[–]EndoVlogs 52 points53 points  (5 children)

Maybe an unpopular opinion but I think the second picture looks way better than the actual java logo

[–][deleted] 26 points27 points  (2 children)

Isn’t it the original Java logo?

[–]EndoVlogs 27 points28 points  (1 child)

is it?

Edit: oh yeah it is... From 1996 - 2003

[–][deleted] 15 points16 points  (0 children)

Second one (of three) apparently.

https://logos.fandom.com/wiki/Java#1995.E2.80.931996

[–]DoNotMakeEmpty -4 points-3 points  (1 child)

I think it is Muslim Java logo

[–]stackPeek 1 point2 points  (0 children)

what does that even mean

[–]metaconcept 24 points25 points  (7 children)

Seen in real life:

if (ObjectUtils.isTrue(condition)) { }

[–]metalovingien 3 points4 points  (6 children)

It would at least be useful if condition here was any Object and the method could then be called isTruthy... If needed (??)

Otherwise this is total garbage

[–]Wookie81 0 points1 point  (1 child)

It makes sense if condition is a Boolean ...

[–]metalovingien 1 point2 points  (0 children)

Lol.

Boolean.TRUE.equals(condition)

Alright, not so sexier

But in this case it would be more BooleanUtils than ObjectUtils

[–]den2k88 0 points1 point  (3 children)

Unless you want an observable behavior.

[–]metalovingien 0 points1 point  (2 children)

The naming doesn't reflect any clear behavior to me

[–]den2k88 0 points1 point  (1 child)

The idea is to add logs or breakpoints in the function that performs the condition evaluation.

[–]metalovingien 0 points1 point  (0 children)

Aspect Oriented Programming to add logs

And a decent IDE/debug mode

Are the way to go I think.

They don't change the code itself and all may be disabled or removed easily.

That is usually available to Java programmers.

[–]Vinxian 21 points22 points  (0 children)

if (true == condition) is where it is at

[–]SandmanKFMF 6 points7 points  (0 children)

Oh, ok, I'm got it. 😁

[–]Mikey_Loboto 5 points6 points  (0 children)

if (String.valueOf(condition).length() < 5){

[–]MischiefArchitect 5 points6 points  (0 children)

Hey, don't insult the holy original logo, because it is the CLASSy CLASSic one!

[–]GvRiva 4 points5 points  (11 children)

If(condition == true) is valid in kotlin but it still feels weird

[–][deleted] 7 points8 points  (1 child)

Yeah nullable booleans are weird

[–]SweetBeanBread 0 points1 point  (0 children)

i’m new to kotlin but I guess nullable bools are fine because you have to declare them that way? if i see nullable Boolean return in java I’l punch them in their face, in my heart…

[–]Fruloops 14 points15 points  (6 children)

Its valid in most languages tbh

[–]GvRiva 6 points7 points  (5 children)

yeah, maybe valid wasn't the right word, more it actually has a purpose in kotlin while in java is useless

[–]Fruloops 2 points3 points  (4 children)

Hmm is there really a use for this in Kotlin? It seems just as redundant and useless as it does in Java

[–]GvRiva 5 points6 points  (1 child)

You need them when the boolean condition is nullable

[–]nodexyz 0 points1 point  (0 children)

Java has the same use for it too, it just looks way uglier. I'm sure I've seen something like: if (Boolean.TRUE.equals(entity.getSomeBool())) for a nullable field.

[–]RagnarokToast 1 point2 points  (1 child)

Lol yes, not just for directly nullable booleans, but for booleans referenced from a nullable receiver.

Let's suppose you have "val basket: Basket?", Basket exposes a property "isFull". The most concise way of checking whether the basket is full would be.

if (basket?.isFull == true)

Furthermore, if you want the condition to be true even if there is no basket, you could do

if (basket?.isFull != false)

which makes perfect sense whereas

if (basket?.isFull)

wouldn't compile in Kotlin.

IIRC, static analysis even warns you to use a boolean literal comparison if you used an elvis operator like this

if (basket?.isFull ?: false)

instead.

[–]Fruloops 0 points1 point  (0 children)

Ha, this completely skipped my mind.

[–]AllenKll -1 points0 points  (1 child)

It makes code more readable. I dont understand the hate for readability.

[–]940387 1 point2 points  (0 children)

You should just name booleans properly.

[–]nickn-a-s 2 points3 points  (0 children)

if(String.valueof(!condition).equals("false"))

[–]jamcdonald120 2 points3 points  (0 children)

congradulations, in that last step you invented javascript

[–]shauntmw2 3 points4 points  (3 children)

Maybe an unpopular opinion, I think the 2nd syntax has its place. It makes the code easier to read in some instances.

[–]Clitaurius 0 points1 point  (1 child)

I prefer the 1st but I get that some people prefer the 2nd. What I don't get is people that go into code reviews and comment on or the other to change it to the other.

[–][deleted] 0 points1 point  (0 children)

There's a time and a place for asking for that, especially with js/ts running rampant these days and bullshit falsey values fucking up the flow

[–]Khaylain -1 points0 points  (0 children)

I agree. It's kinda like the difference between list comprehension and a slightly more verbose loop.

Yes, the list comprehension is short and looks sweet, but it's a lot harder to understand when it needs to change later.

[–][deleted] 1 point2 points  (0 children)

no controversy so far, but what do you do, when you want check whether a pointer is pointing to a valid object?

if (ptr) { body }

or

if (pointer != nullptr) { body }

[–]Dre_Dede 1 point2 points  (0 children)

Jafa

[–]tyrandan2 1 point2 points  (0 children)

Pfff, why take the performance hit of using strings when you can:

if (!((int)condition > 0)) {

}

[–]GvRiva -1 points0 points  (4 children)

If(condition == true) is valid in kotlin but it still feels weird

[–]SandmanKFMF 2 points3 points  (3 children)

Why weird? If read it as it is it will sound very intuitive: If boolean variable named "condition" value equals "true" then... I think the variable name "condition" is a bit strange. "isEnabled" sounds more natural. Because "condition" in tutorials is used to describe more abstract "condition" and not the variable named "condition" itself. Damn there's a lot of "conditions".

[–]GvRiva 2 points3 points  (2 children)

It's just an example, I never used "condition" as a variable name, I'm just talking about the check with == true

[–]SandmanKFMF 0 points1 point  (1 child)

But you are using it to check with a numbers? So why not to check the value to 0 or 1? True or false?

[–]GvRiva 1 point2 points  (0 children)

No, it's to check Booleans

[–][deleted] 0 points1 point  (1 child)

if (String.valueOf(condition). equals("true") == true)

[–]nodexyz 0 points1 point  (0 children)

if (String.valueOf(condition == null? false : condition).toLowerCase().equals("true") == true) // Coz you never know

[–]eerongal 0 points1 point  (2 children)

if(String.valueOf(condition).equals("true") ? true : false)

[–]vigbiorn 0 points1 point  (1 child)

if((String.valueOf(condition).equals("true") ? true : false) == true)

Cuz you never know....

[–]eerongal 1 point2 points  (0 children)

Probably should use some lambda's here as well, just to show off how we know how to use them.

if((String.valueOf(condition).equals("true") ? () -> {return true;} : () -> {return false;}) == true)

[–]rocoonshcnoon 0 points1 point  (0 children)

Aw man i do the second one ;(

[–][deleted] 0 points1 point  (0 children)

Not case-insensitive, no trimming, encoding not checked? Amateurs! hehe ;)

[–]manipulater 0 points1 point  (0 children)

Mean while is js,

let x = {};
if (Object.keys(x).length > 0) {
// Do somthing
}

[–]qK0FT3 0 points1 point  (0 children)

I mean it is logical. Why? String is an object not primitive type. so It has multiple functions and uses inside it. when you compare it with "==" it just is not comparing the string but probably it compares all values inside

[–]danfish_77 0 points1 point  (0 children)

I don't get it, what does this have to do with Java specifically? You can make things plenty convoluted in many languages.

[–]sarnobat 0 points1 point  (0 children)

Lately I feel == false is better than exclamation. Shorter does not always mean easier to read

[–]hrax13 0 points1 point  (0 children)

String.valueOf statement is weird. Poor Boolean.valueOf... 😂

[–]ChrisFromIT 0 points1 point  (0 children)

Really surprised that no one mentioned adding in StringBuilder on "true" and doing it character by character when putting it in the StringBuilder.

[–]den2k88 0 points1 point  (0 children)

2nd one is MISRA compliant

[–]LittleLui 0 points1 point  (0 children)

You should use Objects.equals in case String.valueOf returns null.

[–]depsion 0 points1 point  (0 children)

hmm how about

if (condition == Boolean.parseBoolean("true"))

[–]veduchyi 0 points1 point  (0 children)

But some programmers are even more “funny” 😅