True crime

123 points · 19 comments · view on lemmy.world

19 Comments

bleistift2@sopuli.xyz · 32 pts · 1y (2 replies)

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 · 29 pts · 1y (1 reply)

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, because the log will give you nonsense.

bleistift2@sopuli.xyz · 5 pts · 1y

Good point.

potatoguy@potato-guy.space · 25 pts · 1y

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 pts · 1y

Yes JS is the crime in itself.

zaphodb2002@sh.itjust.works · 13 pts · 1y

Not yes or no, but a secret third thing

Natanox@discuss.tchncs.de · 11 pts · 1y (2 replies)

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

ferric_carcinization@lemmy.ml · 8 pts · 1y (1 reply)

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

Edit: Looks like TypeScript, but the same applies.

Venat0r@lemmy.world · 2 pts · 1y

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

20inmyhead@lemmy.ml · 8 pts · 1y

Like any good Boolean:

True / False / File not found

fubarx@lemmy.world · 4 pts · 1y

True, false, and magic.

HiddenLayer555@lemmy.ml · 4 pts · 1y

Just use an enum lol

punkcoder@lemmy.world · 3 pts · 1y (2 replies)

the real crime in this photo is the ===

nailbar@sopuli.xyz · 2 pts · 1y (1 reply)

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

Doxin@pawb.social · 3 pts · 1y

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

NigelFrobisher@aussie.zone · 3 pts · 1y

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

pineapple@lemmy.ml · 2 pts · 1y (1 reply)

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

nailbar@sopuli.xyz · 9 pts · 1y

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

meekah@lemmy.world · 2 pts · 1y

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

HelloRoot@lemy.lol · 1 pts · 1y
[ removed ]