76
u/Ignisami 10d ago edited 10d ago
You're missing
if (Boolean.TRUE.equals(condition)) which is what SonarQube's default Java rules will recommend. (edit: in case anyone wonders, it's because of null safety; if (condition) throws an NPE if condition is null, Boolean.TRUE.equals(condition) just evaluates to false)
39
u/NoWeHaveYesBananas 10d ago
What if you want it to throw an error, and not hide your bugs like a coward
20
u/Ignisami 10d ago
try { if (condition) { /* code whatever */ } } catch (MommaAintRaisedNoCowardException _) { log.error("No bugs to be seen here no sirreee"); } public class MommaAintRaisedNoCowardException extends NullPointerException { /* standard exception stuff */ }
21
u/yousirnaime 10d ago
if( LlmService.request(" does this seem truthy, respond only 'yes' or 'no' : " . condition) == '{"response":{"message":"yes"}}'){
}
13
u/Simple-Olive895 10d ago
if (myBool == true) {
return true}
2
u/xaranetic 9d ago
package check.true; public class TrueCheck { public enum Truth { TRUE; public boolean asPrimitiveBoolean() { return Boolean.parseBoolean(this.name().toLowerCase()); } } public static class True { private final Truth truth; private True(Truth truth) { this.truth = truth; } public static True getTrue() { return new True(Truth.TRUE); } public boolean value() { return this.truth.asPrimitiveBoolean(); } } public boolean checkIfTrue(boolean myBool) { if (myBool == true) { return True.getTrue().value(); } return !True.getTrue().value(); } }
15
u/Varderal 10d ago
Reminds me of one time in a class. I needed to get from one datatype to another with no clear way... I kinda did "variable.toThis.toThat.toAnother.toTarget". Asked rhe teacher if there was an easier way and he just looked at it and went "the fuck is that?" I was all "exactly!" XD
3
u/realmauer01 9d ago
Sometimes that happens in java, you have to transform stuff to a stream that stream to an array and then you can do stuff like for each or map.
6
4
3
3
u/MallAgreeable5538 7d ago edited 7d ago
Istrueclass istrueobject(valuetrue);
if(istrueobject.istrue(boolean)){
2
1
u/NoodlyGirl2000 10d ago
I do have a lot of fond memories of Java, but I also think C# does it so much better
1
u/induality 9d ago edited 9d ago
The fourth one isn’t even correct. The comparator compare method returns an int. You need to check that that return value equals zero.
1
1
u/DONARUDO_TURANPU 9d ago
Doesn't Java have some ternary operator like JS? That would be the first image but in full HD 3D
1
1
0
130
u/y0shii3 10d ago
How you do it
vs how your teacher does it
vs how your classmate does it
vs how Spring devs do it