True crime

submitted by

https://europe.pub/pictrs/image/d0d6eb20-6c6e-4d50-81d1-29f753d8722f.png

True crime
19
118

Log in to comment

19 Comments

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.

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.

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.

Yes JS is the crime in itself.

Not yes or no, but a secret third thing

Like any good Boolean:

True / False / File not found

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

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

Edit: Looks like TypeScript, but the same applies.

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

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

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

the real crime in this photo is the ===

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

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

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

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

True, false, and magic.

Comments from other communities

I mean aside of the variable name, this is not entirely unreasonable.

I would certainly rather see this than {isAdmin: bool; isLoggedIn: bool}. With boolean | null, at least illegal states are unrepresentable... even if the legal states are represented in an... interesting way.

Admin false LoggedIn false doesn't feel illegal to me, more redundant if anything

I was thinking of the three legal states as:
- not logged in (null or {isAdmin: false, isLoggedIn: false})
- logged in as non-admin (false or {isAdmin: false, isLoggedIn: true})
- logged in as admin (true or {isAdmin: true, isLoggedIn: true})

which leaves {isAdmin: true, isLoggedIn: false} as an invalid, nonsensical state. (How would you know the user's an admin if they're not logged in?) Of course, in a different context, all four states could potentially be distinctly meaningful.

ah you are right! i am so dumb.

Honestly logged in is state and shouldn't be on the user object.

E: omg forget my whole comment. I agree with you that the name sucks.


I mostly don't like that role is typically an intuitive name, and now suddenly it means something I wouldn't expect. Why add confusion to your code? I don't always remember what I meant week to week, much less if someone else wrote it.

If I had a nickel for every time that happened to me, I’d still be poor, but at least I’d have several nickels. 😁

The variable name is 90% why this is so unreasonable. Code is for humans to read, so names matter.

Product manager: "I want a new role for users that can only do x,y,z"

Developer: "uh.. yeah. About that... Give me a few days."

Hmmm I need a datatype with three states... Should I use a named enum? No, no that's too obvious...

Yeah let’s use a union of a boolean and null to represent role, something that inherently represents more than two (…or three, I guess) different values, as opposed to something like an integer.

Even if the name is clearly misleading in this specific case, the entire choice of using a bool here is just bad because it’s almost guaranteed you’re going to expand on that in future and then you’ll just have to entirely rewrite the logic because it simply can’t accommodate more than two values (or three with the null union… 🙈), while it gives absolute zero benefits over using something more reasonable like an integer to represent the roles, or in this case, admin, not-admin and guest. Even if you’ll end up with just admin, non-admin and guest, the integer would still work great with no disadvantages in terms of amount of code or whatever. Just increased legibility and semantical accuracy.

Not to mention that there’s zero reason to combine the state of being logged in and the role in which you’re logged in in one variable… those are two different things. They will remain two different things in future too…

I mean they’re already chaining elseifs (basically matching/switching, while doing it in an inefficient way to boot 🥴) as though there were an n amount of possible states. Why not just make it make sense from the start instead of whatever the hell this is?

This is quite reasonable, aside from the variable name which should be isAdmin. A user either is an admin, or isn't. Unless we don't know, then it's null. You are correct this is bad if the point was to represent roles, but it's not supposed to.

Admin is a role though, was my point. And besides, if you check for three different states, and you decide to go with a boolean to represent that, I really find it hard to believe anyone would think it reasonable. It’s valid and it’s practical, but can you really say it’s reasonable?

I don’t do typescript, but wouldn’t a union of a null and a bool be just more resource intensive than simply using an unsigned byte-sized integer? I struggle to find reasons to ever go for that over something more reasonable and appropriate for what it attempts to represent (3 distinct states as it stands, and likely in future more than just 3 when they have a need for more granularity, as you’d often do with anything you’d need an admin role distinction in the first place), but likely I’m just not familiar with ts conventions. Happy to hear the reasoning for this though.

My preferred way of modelling this would probably be something like
role: "admin" | "regular" | "logged-out"
or
type Role = "admin" | "regular";
role: Role | null
depending on whether being logged out is a state on the same level as being a logged-in (non-)admin. In a language like Rust,
enum Role {Admin, Regular}
instead of just using strings.

I wouldn't consider performance here unless it clearly mattered, certainly not enough to use
role: number,
which is just about the least type-safe solution possible. Perhaps
role: typeof ADMIN | typeof REGULAR | typeof LOGGED_OUT
with appropriately defined constants might be okay, though.

Disclaimer: neither a professional programmer nor someone who regularly writes TypeScript as of now.

Yeah obviously with constants for the set roles per value. Some languages call them enums, but the point is that what we pass and use is always still the smallest integer type possible. With the extra bonus that if the roles ever become composable, the same value type would likely suffice for a bitflag and only thing needing refactoring would be bitshifting the constants.

But anyway, this turns out to be the weirdest hill I find myself willing to die on.

So in a language with nullable types, are you against a boolean ever being nullable? Null means "empty, missing info". Let's say we have role variable with a enum type of possible roles. It could still reasonably be nullable, because in some scenarios you don't know the role yet, like before log in.

In any use case where we need to store some boolean, it's a common occurrence that we don't have the data and it's null. It would be overkill to use an enum with True, False, NoData for these cases, where there is already a language feature made just for that, nullable values.

I've never used TypeScript, just writing from experience in other languages.

Yeah, but if it is about being an admin or not, hence the bool, it’d be idiomatic and reasonable to assume it to be false if we have no data. Unless we want to try and allow admin access based on no data. Having three states for a simple binary state is weird. And if it is not about just being an admin or not, the bool is inherently a too limited choice for representation.

Depends on your requirements.

If the admin status needs to be checked in a database, but most actions don't require authentication at all, it's pointless to waste resources checking and it would be left null until the first action that needs the information checks it and fills it in as true or false.

Ah, the ol' tristate boolean switcheroo

tristate as in three states or tristate as in five states?

That is the jankiest thing I have seen in at least ten years.

Edit: because *of course* it's office.

Classic checkbox values

Yup. Checked, unchecked, and not checked.

This is pretty clearly just rage bait. Nothing is actually setting the value so it's undef. Moreover there isn't any context here to suggest if the state definitions are determined by some weird api or are actually just made up

Troof

I mean facts. Facts is what the kids say. Facts.

*Fax

We don't use fax machines any more grandad! It's all twoggles now! Twoggle me a nurp!

Sadly this is (or used to be) valid in PHP and it made for some debugging “fun”.

There are several small details that PHP won't allow, but It's valid Javascript and it's the kind of thing you may find on that language.

What if role is FILE_NOT_FOUND?!

if it's 'FILE_NOT_FOUND' then the string will be read as truthy and you will get 'User is admin' logged.

Ackshually three equal signs check for type as well. So mere truthiness is not enough. It has to be exactly true.

Also, everyone knows FILE_NOT_FOUND isn't a string but a boolean value.

i would say why would you just not to isAdmin = true but i also worked with someone who did just this so i'll instead just sigh.

also the real crime is the use of javascript tbh

That's TypeScript. I can tell by the pixels defining a type above.

Was looking at it and could not figure out why their weren't any semicolon's.

Neither Javascript nor Typescript require semicolon, it is entirely a stylistic choice except in very rare circumstances that do not come up in normal code.

Explanation for nerds

The reason is the JS compiler removes whitespace and introduces semicolons only "where necessary".

So writing

js function myFn() { return true; }

Is not the same as

js function myFn() { return true; }

Because the compiler will see that and make it:

js function myFn() { return; true; }

You big ol' nerd. Tee-hee.

That's terrifying, especially in JS where no type system will fuck you up for returning nothing when you should've returned a boolean.

Common JavaScript L

Not wrong, but funnily enough, it's a linting rule win. I'd go nuts if I didn't have my type checks and my linters. My current L, though, is setting up the projects initially and dealing with the configuration files if I raw dog it, but that's a problem with ESLint configs and the ecosystem as a whole having to deal with those headaches. So in the end, the JS devs got clever and shifted the blame to the tooling. 😅

That's good to know. Don't know how I didn't know this. Been writing JS since 2000. Always just used them I guess. Ecmascripts look funny to me without them

Same here. My brain interprets them as one long run-on sentence and throws a parsing error.

Fair enough, I like it better without but I don't have a strong preference and have no issue adapting to whatever the style of the repo is.

I learned about it researching tools to automatically enforce formatting style and came across StandardJS, which eliminates them by default.

I can see the benefit of matching style when working with others. I only code for myself and never had to worry about conformity for project consistency.

It is good to learn new things.

I'm sure I have some coding habitats that would annoy others.

Hmm, a webdev colleague said he'd normally prefer without semicolons, but used them anyways for better compile errors.

Interesting, I'm not aware of any way they would affect compile errors. I'd be curious to know more.

I don't have experience with how it affects JavaScript specifically, but independent of programming language, it usually removes the guesswork where the error might be.

The thing is that compilers use fairly static rules for their grammar. So, even if you just typo a comma where there should've been a dot, then its grammar rules don't match anymore and it doesn't really know what to do with your code.
To some degree, it's possible to say certain symbols just cannot appear in a specific place, but especially with a comma, it could be the start of the next element in a list, for example.

Without semicolons, it's likely going to tell you that something's wrong between somewhere before your typo and the next closing brace (}). With semicolons, it can generally pinpoint the specific statement where the comma is wrong.
This should also make analysis quicker while you're editing the code, as it only has to check one statement (or two, if you're inserting a new line and haven't typed the semicolon yet).

role is never instantiated, so the... privileged....*logs*.... will never be called

Edit: Actually no logs at all, I read the null as undefined on first skim

You could make it even dumber by using weak comparisons.

Same as ?

```C++
std::optional<bool> role;

if (role.value())
{ std::cerr ("User is admin");}
else if (!role.value())
{ std::cerr ("User is not admin");}
else if (!role.has_value())
{ std::cerr ("User is not logged in");}
```

Here has_value() should have been checked first, but the JS seems kinda fine.
Which is it?

a === b returns true if a and b have the same type and are considered equal, and false otherwise. If a is null and b is a boolean, it will simply return false.

I see, so logically it is fine.
Just not in the context.

Insert image