121
True crime (lemmy.ml)
submitted 1 month ago by tfm@europe.pub to c/programmerhumor@lemmy.ml
all 20 comments
sorted by: hot top controversial new old
[-] bleistift2@sopuli.xyz 34 points 1 month ago

The logic is fine. If you rename the variable to isAdmin, it makes perfect sense. Either they are an admin, or they are not an admin, or the state is unknown (here expressed as null). If you want to throw another JS-ism at this, undefined could be assigned before the check has been made.

I regularly use variables like this. If users is undefined, I haven’t fetched them yet. If they’re a list, then fetching is complete. If they’re null, then there was an error while fetching.

[-] HelloRoot@lemy.lol 31 points 1 month ago* (last edited 1 month ago)

The only flaw is that the console.log states that null means user is not logged in.

If there are three or more explicit states, you should not use a nullable bool, but some more explicit data structure, like enum.

For example, if the state comes from a db, the user could be successfully logged in, but somehow for a range of possible reasons this variable ends up as null and you'll have a hell of a time debugging.

[-] bleistift2@sopuli.xyz 5 points 1 month ago

Good point.

[-] potatoguy@potato-guy.space 24 points 1 month ago

Today i have seen:

if (var === true || var === 'true' || var === "true")

I'm just fortunate enough to not work with the frontend at our very backend service, but I always hear things that shouldn't be even allowed in this planet.

[-] AnnaFrankfurter@lemmy.ml 16 points 1 month ago

Yes JS is the crime in itself.

[-] zaphodb2002@sh.itjust.works 13 points 1 month ago

Not yes or no, but a secret third thing

[-] Natanox@discuss.tchncs.de 10 points 1 month ago

How many "equal" symbols do we need to be absolutely sure?

[-] ferric_carcinization@lemmy.ml 8 points 1 month ago* (last edited 1 month ago)

It's JS, so about 5, I think.

Edit: Looks like TypeScript, but the same applies.

[-] Venat0r@lemmy.world 2 points 1 month ago

the fun/horrible thing about typescript is you can just start doing js stuff as any time you like...

[-] 20inmyhead@lemmy.ml 8 points 1 month ago

Like any good Boolean:

True / False / File not found

[-] HiddenLayer555@lemmy.ml 4 points 1 month ago* (last edited 1 month ago)

Just use an enum lol

[-] fubarx@lemmy.world 4 points 1 month ago

True, false, and magic.

[-] NigelFrobisher@aussie.zone 3 points 1 month ago

Obviously this should be a Result that the gets compiled to a nullable bool.

[-] meekah@lemmy.world 2 points 1 month ago* (last edited 1 month ago)

Aren't the first two if branches completely useless? The variable was just defined without a value, so it should always be null

[-] punkcoder@lemmy.world 2 points 1 month ago

the real crime in this photo is the ===

[-] nailbar@sopuli.xyz 2 points 1 month ago

The === is the best! I want to know they're really the same and not just evaluate the the same.

[-] Doxin@pawb.social 2 points 1 month ago

any sane language has == behave that way. Javascript == is just completely bonkers.

[-] pineapple@lemmy.ml 2 points 1 month ago

I'm so confused. I understand the reasoning behind two equal signs but three?

[-] nailbar@sopuli.xyz 11 points 1 month ago* (last edited 1 month ago)

Afaik with three it also requires the types to be equal. Like 0 == false, but 0 !== false, because they're different types.

this post was submitted on 14 Aug 2025
121 points (96.9% liked)

Programmer Humor

38555 readers
256 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 6 years ago
MODERATORS