r/badcode • • Jan 21 '21

js It's true

Post image
1.1k Upvotes

33 comments sorted by

142

u/RapeApe Jan 21 '21

Found this snippet in our production code. Twice.

14

u/_justpassingby_ Jan 21 '21

No way man, come on... I have to say it: there's no way this wasn't intentionally written as a joke! The line is here! No further!

10

u/RapeApe Jan 21 '21

I wasn't entirely accurate. That code was in one of the source files twice. I've since found that It also shows up in a different source file.

Callback? Rule of three?

1

u/JanRakietaIV Jan 21 '21

maybe just a running joke

2

u/[deleted] Jan 21 '21

that's worrying

0

u/[deleted] Jan 21 '21

[deleted]

59

u/RapeApe Jan 21 '21

+1 for keeping the == operator intact! :p

You can probably guess...this code stores all of its state as text in hidden inputs. So this code basically translates between '1', '0' and 'true', 'false'

4

u/Disturbed147 Jan 21 '21

Ditch jQuery and use vanilla. Then you've got the correct way

39

u/jmswlltt Jan 21 '21

To be fair !!istrue will always be true

40

u/RapeApe Jan 21 '21

...hence the post title ;)

It turns out istrue was never actually referenced anywhere else in the code, which is why it's underlined with a squiggly... (no-unused-vars)

2

u/PrydeRage Jan 21 '21

And here I was thinking that squiggly line was because the line isn't terminated with a semicolon (Yes I know JS allows this but I don't)

3

u/RapeApe Jan 21 '21

I use standardjs.

Takes a little getting used to, but once you do, semicolons seem weird.

-6

u/eavab Jan 21 '21

Never double bang bro.

3

u/fynn34 Jan 21 '21

Double bangs are handy, why would you not double bang?

66

u/ce-walalang Jan 21 '21

Image Transcription: Code


var istrue
if ($('#isEdit').val() == 1) {
  istrue = 'true'
} else {
  istrue = 'false'
}

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

6

u/Flawlesscode Jan 21 '21

good hooman

18

u/Farsqueaker Jan 21 '21

Thanks, I hate it!

6

u/SaintFuckNugget Jan 21 '21

We've all been there on a rainy Monday morning

6

u/zimoupouf Jan 21 '21

If istrue == false then isfalse = true

4

u/gristoi Jan 21 '21

Aaaah my eyes

1

u/[deleted] Jan 21 '21

The biggest sin is using jquery in 2021

2

u/RapeApe Jan 21 '21

You say that now, but you haven’t seen the rest of the code... 😱

It’s gonna get a complete rewrite soon, but for now I’m just trying to patch up some show-stopping bugs

1

u/Ahajha1177 Jan 21 '21

So just to get this straight, if they had just used normal, not-string 'true' and 'false' in the first place, all of this code would be redundant?

1

u/GavinZac Jan 21 '21

Just use the 1 and the 0 by themselves instead of wasting all those characters on rue and alse

This post brought to you by Perl Gang

1

u/RapeApe Jan 21 '21

Problem is, the 1 and 0 are actually ‘1’ and ‘0’, so...same issue

1

u/Ahajha1177 Jan 21 '21

I meant the literal booleans, not 1 and 0, which are technically what they are anyways, but it's better to think of them as booleans directly.

1

u/fleaspoon Jan 21 '21

beautiful

1

u/colorpulse6 Jan 21 '21

Is true = false

1

u/thusman Jan 21 '21

Disappointed there is no isfalse = 'true'

1

u/[deleted] Jan 22 '21

I'm more annoyed that they're not isTrue and isFalse. CamelCase please.

0

u/[deleted] Jan 21 '21

[deleted]

1

u/Golden_Lynel Jan 21 '21

Genuinely asking, whats wrong with it?

0

u/[deleted] Jan 21 '21

[deleted]

1

u/Golden_Lynel Jan 21 '21

Which part? The "else"?

10

u/no1adamb Jan 21 '21

Really wish people wouldnt delete useful comments