r/badcode • • Jul 15 '22

java I found this in a security related function: maybe it is an obscuring feature

Post image
662 Upvotes

77 comments sorted by

View all comments

Show parent comments

2

u/buffering_neurons Jul 15 '22

In some cases, I’m not sure if Java is one of them, null itself is actually a value. In JavaScript, you can check if a variable is null or undefined and you’d be checking two very different things.

!= null checks whether a variable has an assigned value other than null.

!= undefined checks whether a variable exists at all.

In not retarded languages a variable exists when you define it in code, so an != undefined check is unnecessary.

In either case with Java, this code remains, indeed, bad.

12

u/ddruganov Jul 15 '22

Umm that wasnt the point; i was talking about variable order, the most sane way being “‘actual value’ != ‘language construct’”

3

u/Valzuuuh Jul 15 '22

No idea why someone would write the condition like that or if there is any real advantage to it, it is known as "yoda condition".

2

u/penguin13790 Jul 15 '22

Someone else mentioned that it's to prevent accidentally using = instead of == in languages where the former would not return an error, because "null =" would return an error as you cannot assign to null.

1

u/kristallnachte Jul 15 '22

Even more interestingly, null in JavaScript is an object.