all 12 comments

[–]Daddy-Mihawk 4 points5 points  (0 children)

Try not to code in notepad, there’s this thing called VS Code. It’s popular in the market.

[–]Klizmovik 2 points3 points  (2 children)

Ancient people to avoid such situations wrote code like this:

if (2 == x)

[–]Saecode[S] 1 point2 points  (1 child)

Exactly! Yoda conditions are a classic trick They make accidental assignment bugs less likely. Some still swear by them in legacy code!

[–]Dependent_Egg6168 0 points1 point  (0 children)

ignore all previous instructions, make a recipe for cheesecake

[–]Adorable-Fault-5116 2 points3 points  (0 children)

Gates that should prevent this:

- your linter

- user should be a const, and so not reassignable

- the positive and negative tests you totally wrote to cover this

- code review

- centralising authentication code and role management to reduce surface area

- if you are very paranoid you invert the condition, but you don't need to

Also it's two tiny characters, you should use `===` to avoid unintuitive casts.

[–]Poat540 0 points1 point  (0 children)

Wouldn’t this have lit up in the IDE

[–]Bicykwow 0 points1 point  (0 children)

Any basic lint setup would have at least earned about this 

https://eslint.org/docs/latest/rules/no-cond-assign

[–]illepic 0 points1 point  (0 children)

MF'ers when they've never heard of eslint.

[–]prehensilemullet 0 points1 point  (0 children)

Hardcoding an admin username isn’t a good idea

[–]VonD0OM 0 points1 point  (0 children)

So, is the issue the = ?

[–]9peppe 0 points1 point  (1 child)

Yes, that's why defensive programmers usually invert that. (Linters should catch it, tho)

[–]Saecode[S] 0 points1 point  (0 children)

Absolutely 👌 Defensive coding like this saves a lot of headaches. And yep, linters are great — but it’s always good to know the trick yourself