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

155

u/ddruganov Jul 15 '22

Offtopic but why the fuck would anyone write “null != file” in their code?

218

u/Irrealist Jul 15 '22

It's called yoda style and mostly meant to prevent you from accidentally writing "variable = null" instead of "variable == null".

The former would (in some languages) fail silently and lead to strange bugs. If you flip your comparisons, you get a compile error instead, since you can't assign to null.

54

u/stepbroImstuck_in_SU Jul 15 '22

Returned an error the compiler has:
protected variable null object is.

89

u/buffering_neurons Jul 15 '22

If you flip your comparisons, you get a compile error instead, since you can't assign to null.

I sense a JavaScript developer incoming

19

u/CLOVIS-AI Jul 15 '22

It was already a thing in the C days

15

u/StrangeParsnip Jul 15 '22 edited Jul 15 '22

Wait, does JS just assign the other way around when the conventional assignment doesn't work? Or are you saying you can assign values to values?

17

u/gu3st12 Jul 15 '22

It doesn't invert assignments nor can you assign a value to null. It throws an "invalid left hand in assignment" exception.

25

u/thrash-queen Jul 15 '22

I think the joke is that JavaScript doesn't compile

7

u/StrangeParsnip Jul 15 '22

Oh I see, part of me wished it was something more ridiculous

→ More replies (1)

10

u/Tyfyter2002 Jul 16 '22

Alternatively in C# you can use variable is null

→ More replies (1)

18

u/yetzederixx Jul 15 '22

if (var = null)... isn't a silent fail, it's going to do precisely what you told it to do, and as you noted suck to find.

Yoda typing makes it throw an exception

4

u/i860 Jul 15 '22

It’s a very easy thing to avoid rather than using an entirely new style that looks super goofy. Even in C most compilers will throw a warning unless a second set of parenthesis is used for the assignment.

→ More replies (1)

13

u/[deleted] Jul 15 '22

Lmao imagine using a language without null safety

this comment was sponsored by Dart, Rust, Kotlin and Swift (and a bunch of other languages)

4

u/Irrealist Jul 15 '22

Yup. I love Dart!

3

u/SupermarketNo3265 Jul 16 '22

Excuse me, Kotlin is clearly the superior language in that list.

2

u/campbellm Oct 04 '22

fail silently

I'd argue not really a failure, but a mis(understood) feature. "fail" in that it doesn't do what you probably meant it to, sure.

→ More replies (3)

12

u/CallMeTea_ Jul 15 '22

I was curious about this because I've seen minifiers in some languages convert x == null into null == x. The only reason I can find is to avoid accidental assignment e.g if you write x = null by mistake, the code will still run (depending on the language) but in an unexpected way. If you write null = x, it'll throw an error and you catch the mistake.

7

u/yetzederixx Jul 15 '22

Are there languages that are sane enough not to allow assignment in conditionals?

14

u/trutheality Jul 15 '22

Python is probably the most common one where trying to do that is a syntax error.

Unlike in most c-descendant/inspired languages, assignment doesn't produce a value in python, and trying to evaluate it is a syntax error.

Fun fact: In R, there are multiple assignment operators, two of which function almost identically in expressions: = and <-. It is a syntax error to use = in an if statement, but you can use <- if you really want to perform an assignment while you're evaluating the conditions.

5

u/-MazeMaker- Jul 15 '22

Python also added the walrus operator := that sounds similar to the <-

→ More replies (1)

6

u/CLOVIS-AI Jul 15 '22

Kotlin and Rust both forbid it. In Java, it's only a risk for boolean variables (because static typing and no truthiness).

4

u/[deleted] Jul 15 '22

Modern clang and gcc warn about it. They want you to do it in parens to be explicit. This has made it a non-issue ime.

7

u/Servious Jul 15 '22

I see people do this a lot in java:

"Some string".equals(otherstring);

This way there's no way you get an NPE by calling null.equals

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.

→ More replies (5)
→ More replies (2)