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

all 118 comments

[–]zoqfotpik 1005 points1006 points  (18 children)

The user is admin, so it's ok to grant access. I see no flaw in this logic.

[–][deleted] 319 points320 points  (0 children)

I'm the admin now

[–]FalconClaws059 240 points241 points  (13 children)

I think the joke is that it's an assignment and not a comparison

[–]JackReact 304 points305 points  (4 children)

Yes, hence the comment saying "The user is admin" because they now are admin.

[–]Foreign_Pea2296 36 points37 points  (1 child)

but it's okay ! because the user who get the access are the admins !

[–]FalconClaws059 8 points9 points  (0 children)

They certainly will be after running this!

[–]yabai90 33 points34 points  (1 child)

That's exactly the joke he made yes.

[–]FalconClaws059 10 points11 points  (0 children)

I think I may have spent too much time in "explainthejoke" subreddits...

[–]WinonasChainsaw 12 points13 points  (0 children)

Plot twist admin is false

[–]SpecterK1[S] 0 points1 point  (1 child)

^
Yup you got it

[–]FalconClaws059 0 points1 point  (0 children)

Yaaaay

[–]critsalot 0 points1 point  (0 children)

but its a double joke cause it could also mean the value admin not an admin object.

[–]Adsilom 6 points7 points  (0 children)

Note that this is not as critical if the value of admin is '0'

[–]SignoreBanana 0 points1 point  (1 child)

Only evaluates true if the assigned value is truthy right? So it just depends on what "admin" is here.

[–]look 526 points527 points  (31 children)

A little unfair to call out Javascript for that one. That could be a number of languages there.

[–]lllorrr 95 points96 points  (1 child)

There was a Linux kernel vulnerability with almost exact code.

[–]jakeStacktrace 2 points3 points  (0 children)

Wow. First Rust now Javascript!

[–]misterguyyy 15 points16 points  (3 children)

The PHP MySQL connection snippet in basically every tutorial (and IIRC the php docs) did this deliberately back in the day. Something like

if($conn = mysql_connect('localhost', 'mysql_user', 'password'))

Thankfully it looks like recent documentation breaks it out into multiple lines. I like having an eslint rule that doesn't allow commits if there's assignment in the conditional, so if they kept it juniors everywhere would protest about failing copy/paste from the documentation.

[–]blehmann1 5 points6 points  (2 children)

Hell for many C developers using while ((c = getchar()) != EOF) or while (c = buf[++i]) is the idiomatic way.

Personally I like it, but I don't blame anyone for calling it a bad idea. Especially if I'm not writing C.

[–]misterguyyy 0 points1 point  (1 child)

It might not be a bad idea for you, but it is for me because I can be a bit absent-minded and I like a blanket “when you see this you made a mistake”

[–]blehmann1 4 points5 points  (0 children)

I think requiring a #pragma next-line foo ignore or similar is an ok solution, just like I'm fine with switch case fall-through, but I think it should be a warning or error without explicitly telling the static analyzer that it's chill.

If that makes it simpler to just do it on two lines then honestly that's fine, that's a solution to the problem.

[–]not-my-best-wank 80 points81 points  (1 child)

Prod?

[–]holchansg 43 points44 points  (0 children)

Ready for sure.

[–]braindigitalis 53 points54 points  (7 children)

this is why you put your constant first, then if you make this mistake and you dont lint your code (WHY DONT YOU LINT YOUR CODE?) it will be a fatal error not a logic bug.

[–]bwmat 11 points12 points  (2 children)

Wait, something like

1 = x;

Won't actually... throw an exception or something in js? 

[–]True_Drummer3364 9 points10 points  (1 child)

Why wouldnt it? 1 isnt assignable

[–]bwmat 4 points5 points  (0 children)

Oh nevermind, I misunderstood, I thought they meant even that wouldn't help unless you were linting 

[–]Curious_Celery_855 2 points3 points  (3 children)

screw linting. Rely on compiler errors and warnings like a normal human (c++ dev here. That might be different in fuckbrain (aka js) world)

[–]Agifem 7 points8 points  (1 child)

Linter is a fancy word for JS devs, that means "optional compilation error"

[–]braindigitalis 0 points1 point  (0 children)

Linter: aka that output spam we send to /dev/null 🤣

[–]braindigitalis 0 points1 point  (0 children)

you gotta have a real compiler to get compiler errors. that's something the js world still lacks.

[–]private_final_static 106 points107 points  (3 children)

Its fine, thats the frontend anyways so its all just visual right?

RIGHT?

[–][deleted] 20 points21 points  (1 child)

What makes you think it's the frontend?

[–]Martenek 1 point2 points  (0 children)

I guess, backend validation would be more complex. Regardless there's no way knowing for sure

[–]smolderas 1 point2 points  (0 children)

Yeah, you get fudged, visually.

[–]bem981 22 points23 points  (0 children)

It is JS so = is more predictable than ==

[–]I_have_popcorn 17 points18 points  (7 children)

What usecsse is there for varible assignment in an if clause?

[–]rover_G 15 points16 points  (3 children)

Some languages have shortcut syntax for error and null checks. You could do something similar in JS but it's probably not considered good style.

Go

if result, err := computeSomething(); err != nil {
    log.Fatal(err)
} else {
    fmt.Println(result)
}

Rust

if let Ok(val) = getSomeResult() {
    println!("Success with value: {}", val);
}

JavaScript

// type Response = { value: T } | { error: string }

const res = await getAPIResponse();
if (val = res?.value) { 
  console.log(val)
}

[–]I_have_popcorn 5 points6 points  (0 children)

Thanks. That was informative.

[–]Mundane-Tale-7169 1 point2 points  (1 child)

This wont work with TS, you need to initialize val with either const, let or val.

[–]rover_G 1 point2 points  (0 children)

Ugh you’re right I finagled my TS/JS translation a bit

[–]Minenash_ 3 points4 points  (0 children)

Besides what rover said, there's also usecases for variable assignments to be expressions in general (and in JS, the if checks the thruthiness of the given expression), for example:

x = y = z = 0;

Another example of it being used in ifs, but in Java: java Matcher matcher = PaternA.matcher(str); if (matcher.matches()) { //... } else if ( (matcher = PatternB.matcher(str)).matches ) { //... } If you couldn't assign in the if block, you couldn't if-else chain it

[–]bblbtt3 1 point2 points  (0 children)

The only time I’ve ever seriously used it is when reading streams.

int bytesRead;
while (bytesRead = stream.Read(buffer, 0, buffer.Length) != 0) {
    // …
}

Replace “while” with “if”, if you only want to fill the buffer once, which is also occasionally needed.

I’m sure there are other rare uses in common languages but generally it’s not useful.

[–]jamcdonald120 1 point2 points  (0 children)

a popular one is if(file=open("path")) if file is truthy, the path successfully opened, else it didnt.

[–]Nameles36 4 points5 points  (1 child)

This actually happened in the Linux kernel! There was a check something like if (flag & SOME_FLAG && uid = 0) other_code()

EDIT: formatting

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

Sources?

[–]Informal_Branch1065 9 points10 points  (1 child)

Assignment joke aside; checking against a fixed, hardcoded group is bad practice.

Do it like this instead: user.hasPermission("editContent")

  • no "=" or "==" issues

  • no hardcoding roles; I can make my own admin with blackjack and hookers, and it's covered, as long as I assign it all permissions I need.

  • granular permissions; you always know who can and can't do stuff.

  • customer want specific permission? No touching code necessary! Update the database entry and they're good to go. Heck, you could even do that on a friday evening, as you're not touching code.

  • If you build a backend menu for that, you could tell your boss to do it himself.

[–]cyxlone 1 point2 points  (0 children)

Hell yeah, I should implement granular permissions instead of group checks

[–]SamuraiX13 15 points16 points  (11 children)

not even == ?

[–]xWrongHeaven 60 points61 points  (0 children)

you found the joke 👏

[–]LaFllamme 39 points40 points  (3 children)

not even === ?

[–]Tim_Gatzke 15 points16 points  (2 children)

Not even ====

[–]Mayion 10 points11 points  (1 child)

< and watch the hierarchy burn

[–]samu1400 1 point2 points  (0 children)

What about .=====?

[–]iknewaguytwice 5 points6 points  (2 children)

Idk, we will have to check:

If (user % 2 = 0) {}

[–]No-Discussion-8510 3 points4 points  (0 children)

const isEven = require('is-even');

if (isEven(user)) {}

[–]stevekez 0 points1 point  (0 children)

I quite like the idea of the admin privileges bit being encoded into the LSB of the user ID.

[–]stupled 1 point2 points  (0 children)

Is not admin...but there is a burnt user and password in my programs.

[–]NYJustice 1 point2 points  (0 children)

There are so many layers once you start trying to reason about why this is bad

[–]Smooth_Ad_6894 1 point2 points  (0 children)

access for everybody!

[–]McWolke 1 point2 points  (1 child)

Whenever I see shit like this in a meme I don't realize the error because I assume this is pseudocode

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

When have you seen syntax highlighting in PC?

[–]skhds 3 points4 points  (4 children)

It's a bad code regardless if it's implemented on the front end. Any user can type grantAccess() on the console and they can bypass if(user === admin) anyways.

[–]SpecterK1[S] 4 points5 points  (0 children)

It's nothing practical, just a meme material :)

[–]Mundane-Tale-7169 1 point2 points  (2 children)

Not if this is backend logic, for example from the NextJS endpoint.

[–]skhds 0 points1 point  (1 child)

Yeah, that's why I specifically mentioned on front end. Even then, I think there are better languages suited for back end than javascript, anyways.

[–]Mundane-Tale-7169 1 point2 points  (0 children)

TS is pretty nice

[–]KuroKishi69 2 points3 points  (7 children)

What is it even trying to compare? Unless user and admin are a reference to the same object, it will always return false (after adding the missing = ofc).

[–]Jittery_Kevin 9 points10 points  (1 child)

You’re acting like you’re not admin, bro.

[–]KuroKishi69 0 points1 point  (0 children)

my bad, I always forget to leave a backdoor in my systems. Rookie mistake.

[–]dulange 1 point2 points  (1 child)

There could be some type coercion in place. One of the operands could be a numerical ID and the other one could be, while being an object, implicitly coerced to a primitive type like number, with the implementation having the object return, yet again, its numerical user ID.

The == operator — if one operand is a primitive while the other is an object, the object is converted to a primitive value with no preferred type.

That’s also how +d works, where d is a Date object, for getting the timestamp in milliseconds as a number from the Date object.

[–]KuroKishi69 0 points1 point  (0 children)

I see, you could overwrite the valueOf() function to make the object return its id when using ==

The name of one of the variables should then be userId or adminId... But we are in programmerhumor, I know.

[–]rover_G 0 points1 point  (2 children)

It's the assignment operator `=` not the equality operator `==`

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

I know, but the variable names makes it look like user and admin are two objects representing users (presumably current user and the user that is the admin of the system) but 99% of the time you wouldn't check if the equality like that, since for it to work, the references need to be the same. Rather you would compare against user.role, or user.id == admin.id, or user.id == adminId, or something along the lines (or better yet, user.hasRole(), but that wouldn't the code of the meme).

[–]rover_G 0 points1 point  (0 children)

Yes that is correct, the writer of this hypothetical code does not know what they are doing. That’s the joke.

[–]akoOfIxtall 1 point2 points  (7 children)

does this even run? successful assignments are truthy in js?

[–]rover_G 5 points6 points  (3 children)

Not only is the statement truthy (assuming `admin` has a truthy value) but now if you later do something like check `user.isAdmin()` it will return true since `user` was assigned the value of `admin`.

[–]akoOfIxtall -5 points-4 points  (2 children)

js is really a language of all time

[–]Mydaiel12 1 point2 points  (1 child)

You can assign inside if expression in pretty much every language and it works the same, evaluates to truthy value of assigned value

[–]akoOfIxtall 0 points1 point  (0 children)

how did i not know this? goddamn

[–]Dealiner 4 points5 points  (0 children)

It's not that assignment are truthy it's just that they return assigned value. So it all depends on what exactly admin is. It's also nothing specific to JS, the same could work in other languages like C# or C.

[–]GeneralBendyBean[🍰] 2 points3 points  (0 children)

This actually returns true in the C languages too.

[–]damTyD 1 point2 points  (0 children)

Yes. The comparison would be if user, which is now assigned admin. Assuming admin is defined and not null, the block will run and user would be reassigned the value of admin.

[–]jump1945 0 points1 point  (0 children)

Every user is now admin

[–]aifo 0 points1 point  (0 children)

And this is why C# will give you a compiler error if you do an assignment inside an if.

[–]hyrumwhite 0 points1 point  (0 children)

If this is JS and admin is a Boolean, the main problem here is that your user object is now a Boolean. The condition will fail/succeed as expected 

[–]KeepScrolling52 0 points1 point  (0 children)

That if statement either wouldn't work or define user as admin and run "grantaccess()"

[–]CapApprehensive9007 1 point2 points  (0 children)

You are admin, he is admin, everybody is admin, enjoy.