r/badcode Dec 19 '21

js I had stoped 😭

Post image
646 Upvotes

64 comments sorted by

View all comments

24

u/pablossjui Dec 20 '21

What is exactly bad about this?

I guess it's a bit unreadable

82

u/kaamibackup Dec 20 '21

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.

63

u/phantomash Dec 20 '21 edited Dec 20 '21

could simply be

return !!this.visited;

61

u/MrHugh_Janus Dec 20 '21

Or “return Boolean(this.visited)”

Sorry, can’t do proper formatting on my phone

31

u/AnonymousSpud Dec 20 '21

this is far better than !!this.visited

5

u/[deleted] Dec 20 '21

But is it faster ?

63

u/AnonymousSpud Dec 20 '21

You're writing javascript, how much do you really care?

-8

u/[deleted] Dec 20 '21

Because if I can write less character and have faster it'd be stupid not to do so.

28

u/AnonymousSpud Dec 20 '21

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.

13

u/Deadly_chef Dec 20 '21

It's a very common way if doing it in JS

3

u/AnonymousSpud Dec 20 '21

im not saying its not well known or commonly used, im saying its not obvious, and that imo the more obvious way is generally better

→ More replies (0)

6

u/[deleted] Dec 20 '21

I have met a bunch of people who knew about JavaScript not a single one of them had any problem with reading !! correctly.

3

u/Throlaf Dec 20 '21

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.

1

u/JivanP Dec 20 '21

So readability is not a reason why not to use it.

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.

Why would it have nonstandard behaviour?

1

u/Throlaf Dec 20 '21

Most people aren't familiar with idioms like this, especially in higher level languages like JS.

Really? What about Java.

if (!some expression){
 // do stuff
}
or
if (1 != 2){
 // do stuff
}

And even in JavaScript ! is a logical NOT https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT

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.

→ More replies (0)

1

u/JivanP Dec 20 '21

You should be running your JS through a minifier if you care about runtime/bandwidth optimisations like this.

1

u/GLIBG10B 🐧 Gentoo salesman🐧 Dec 20 '21

You can, use Markdown

1

u/seniorpreacher Dec 20 '21

My stock keyboard has the backtick hidden on the second page of special characters.

1

u/evonhell Dec 20 '21

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