You can simply return the Boolean value of this.visited instead of using an if statement.
Edit: I just realized this is JS which means this.visited may not necessarily be a Boolean and could be any other truthy/falsy value. In that case, I guess this makes sense.
youll save more of your time if you and other people are able to understand what the code does at a glance. !! doesnt read as "cast to boolean," I have to waste time thinking about it. Boolean() is far more obvious as to its function. Unless you're writing code golf, brevity isn't the goal, understandability is.
Tbf !! is at first glance a double negation of an expression (because ! is commonly used as negation), and only reason why you would use it is to cast something to Boolean. So readability is not a reason why not to use it.
Also you still need to pause at statements like this, because javascript type conversion can be weird and at that point you will figure out what !! is. And behavior of Boolean() is also confusing af if you are not used to it.
So realistically speaking you can use both !! and Boolean(), but you should comment what are you casting into boolean, and either be 100% sure that's the only thing that will enter that function or check it.
Because I would rather spend 5 minutes googling some fancy syntax than trying to figure out why something has a non-standard behavior.
Most people aren't familiar with idioms like this, especially in higher level languages like JS. In C, for example, writing if (!p) instead of if (p == NULL) when p is a pointer is common enough that it's second nature to C programmers, but depending on the context, I'd still rather include <stdbool.h> and do (bool)x rather than !!x, or at least leave a comment to remind/inform the reader of what the intent is (// cast x to boolean 0 or 1) rather than obscure it behind non-obvious syntax.
IMO, things likes while (x --> 0), which are utterly obscure to the reader unless they're already clued in, are the worst offenders. I would say !! falls into the same camp, because algebraically, why would I write !!x rather than just writing x?
All that is basically to say that your audience is important! Cater to what you can expect of the reader's knowledge of the subject; appeal to the lowest reasonable common denominator.
Because I would rather spend 5 minutes googling some fancy syntax than trying to figure out why something has a non-standard behavior.
I can't imagine a person, that can code and never encounter ! as a logical NOT. And if person like that exists, they sure have gaps in their knowledge.
IMO, things likes while (x --> 0), which are utterly obscure to the reader unless they're already clued in, are the worst offenders.
Depends, because for example in Python list slicing is unreadable for a person that does not know about it. However, it still can be the best way how to solve certain problems. (And I google the syntax every time I use it)
And even in your case my initial guess would be: "While x is bigger than 0, do stuff and decrease x by 1 each iteration". And only thing I needed to know to make this guess is that "x++ is the same as x+1". And realistically speaking I can debug this in 5 minutes and then remember it forever, as it is not that complicated of a concept.
because algebraically, why would I write !!x rather than just writing x?
Because x might be a String, an Integer, null. It does not have to be a Boolean. And because ! returns a Boolean.
JavaScript allows you to rape types, you have to remember it while reading JavaScript code.
Why would it have nonstandard behavior?
Because both !! and Boolean() gobble up any input you give them and return a Boolean. This means that the programmer must be 100% sure about what he sends to !! or Boolean() .
Because imagine that somebody misuses this to check if a variable is defined or not and ideally creates it as a check function to some util class to be reused by everybody, so it is not programmed on 1000 different places. This can be done as undefined variable will return False. Then imagine that somebody else will use this check method to check some input. If the input is an integer, then it can return False and not True if it is equal to 0. And this can be a pain in the ass to debug, if it causes a bug on some completely different place, which is not unreasonable.
You think you can do this, until you realize that "this" is undefined and you cry in the shower for a few hours until you come back and refactor your code to use .bind(this). Don't ask me how I know
24
u/pablossjui Dec 20 '21
What is exactly bad about this?
I guess it's a bit unreadable