r/shittyprogramming • • Sep 25 '14

Yeah, that'll stop em

http://imgur.com/tWScguW
384 Upvotes

61 comments sorted by

16

u/TugOnYourWinky Sep 25 '14

Damn son, where'd you find this?

27

u/amphetamachine Sep 25 '14

It was in an ancient script in a project I inherited. Some guy wrote a set of simple yet arcane and unhelpful functions that I guess he found useful. The file was called smo_scripts.js.

Example function -- who knows what it does?:

function checkUncheckSome(controller,theElements)

20

u/FxChiP Sep 25 '14

Looks like it takes the element ID of a "controller", gets its checked status, then applies it to the checkboxes found in or under (in the DOM tree) the objects named in theElements, which is a string containing a comma-delimited list of element IDs.

24

u/amphetamachine Sep 25 '14

Are you a wizard?

8

u/concatenated_string Sep 25 '14

He's probably been 'round the block a time or 2.

0

u/[deleted] Sep 25 '14

[deleted]

8

u/Madonkadonk Sep 25 '14

but...why?

8

u/Steve_the_Scout Sep 26 '14

It looks like they're trying to emulate x86 assembly instructions?

jge, jle, je, jl, and jg

6

u/Madonkadonk Sep 26 '14

but...why number = number * 1

18

u/moartoast Sep 26 '14

shitty casting to integer?

80

u/Hooogan Sep 25 '14

It would stop my manager.

30

u/MeowMeowWoofWoof Sep 25 '14

Which one? Don't you have 8?

53

u/[deleted] Sep 25 '14

Better make that array a little bigger

restrictWords.push('bewbs');
restrictWords.push('b00bs');
restrictWords.push('b00bs');

26

u/MeowMeowWoofWoof Sep 25 '14

Yes. Bigger is better.

22

u/[deleted] Sep 25 '14 edited Sep 25 '14
var str = 'oo';

setInterval(function() {
    restrictWords.push('b' + str + 'bies');
    str += 'o';
}, 10);

14

u/gypsyface Sep 25 '14

booies?

6

u/[deleted] Sep 25 '14

Oops

9

u/FuckFuckingKarma Sep 25 '14

That's a lovely loop you have going.

I always knew for loops were stupid.

16

u/[deleted] Sep 25 '14

Loops are for the small-minded. This sucker will happily tick along until the sun goes cold.

2

u/revanthedarth Jan 09 '15

or use a regex 'b(0|o)[0o]+b(ie|)s'

51

u/ILoveWubWubs Sep 25 '14

Eh I am just curious here - out of all XXX websites out there, why amateurmatch(dot)com?

110

u/maxximillian Sep 25 '14

I'd have to guess it's the one the author goes to?

42

u/michaelpb Sep 25 '14

Gonna guess his code was being hotlinked by that site, so he put that in there to harass them.

81

u/_Wolfos Sep 25 '14
if(badword==true)

cringe

13

u/Protuhj Sep 25 '14

What's wrong with that?

I know that our coding style at work prefers to be explicit in boolean expressions. Of course, javascript is different than C++...

19

u/yasth Sep 26 '14

Well partly you should === in javascript code, as == will do some magical stuff that is often wrong (it won't happen here, but coding style wise you should avoid it). Stuff like:

"a" == true is false

"1" == true is true

0 == '' is true

Just makes most seasoned devs use === to avoid the possibility.

-3

u/Dragon_Slayer_Hunter Sep 26 '14

When you say magical you make it seem like it does it for no reason or no known purpose. That's not true. == has its purposes.

Basically, == doesn't care about type and allows "truthy" and "falsey" comparisons, and === cares about type. This also makes === a bit faster since it doesn't need to worry about truthy/falsey stuff.

0, empty string, empty array, false, null, undefined, and some other things are falsey, so they will == false. Everything thing not falsey is truthy, so it will == true.

1

u/yasth Sep 26 '14

No obviously it has purposes, magical in the concept of programing languages generally involves "automagic" type stuff. It just is (for a lot of developers) surprisingly magical, as they are used to languages where == is a lot more like ===. (Though === has weird things of its own like "a"===new String("a") being false)

It isn't the speed that is an issue (almost all the time, especially with modern javascript implementations which will effectively write === when things are type controlled like here), it is the subtle weirdness that gets in there, and can cause bugs, especially when == is used for user input.

(Also the more fun one is "0" is false )

6

u/[deleted] Sep 28 '14 edited Sep 28 '14

'a' === new String('a') being false

This is because new String(x) is functionally equal to Object(String(x)) and creates a new String object instead of a String primitive (typeof new String('a') === 'object' is true).

For simple typecasting drop the new.

'a' === String('a') will correctly evaluate to true

Tested on Spidermonkey (Firefox 32.0.3) and V8 (Node.js v0.10.29)

Differences between String objects and String primitives

String primitives:

◄ var a = 'a';

► undefined

◄ a.b = 5;

► 5

◄ a;

► "a"

◄ a.b;

► undefined

Tested on Spidermonkey (Firefox 32.0.3) and V8 (Node.js v0.10.29)

String objects:

◄ var a = new String('a');

► undefined

◄ a.b = 5;

► 5

◄ a;

► "a" (► { '0': 'a', b: 5 } in V8 (Node.js v0.10.29))

◄ a.b;

► 5

Tested on Spidermonkey (Firefox 32.0.3) and V8 (Node.js v0.10.29)

0

u/[deleted] Sep 25 '14

[deleted]

10

u/Protuhj Sep 25 '14

It's just a coding style. If someone changes the type of the variable (for whatever reason), to let's say, an int,

if (variable) {...}

now works for any value that is non-zero.

if (variable == true) {...}  

just shows the intention of the original code. They aren't functionally different, it's just a style.

It also gets easier to read if the if statement has multiple clauses, and when you have multiple contractors viewing/modifying the code, clarity is preferred over brevity.

33

u/[deleted] Sep 26 '14

Good:

if (variable) { ... }

Better:

if (variable == true) { ... }

Best:

if ((variable == true) == true) { ... }

11

u/XL_ARES_IX Sep 26 '14

Besterer:

if((((variable == true) == true) == true) == true)) //just to be safe <|:^)
{ ... }

5

u/RunasSudo Sep 26 '14

Most besterest:

var code = "variable == true";
while (true) {
code = "(" + code + ") == true";
}
eval("if (" + code + ") { ... }");

3

u/pimp-bangin Sep 27 '14

This seems much more intuitive:

function isTrue(expr) {
    return expr == true && isTrue(expr);
}

1

u/XL_ARES_IX Sep 26 '14

this code is OK, but the while loop needs to terminate... I suggest this

while ((((variable == true) == true) == true) == true)) { ... }

4

u/path411 Sep 26 '14

I prefer:

if ((variable == true) ? true : false) { ... }

5

u/Steve_the_Scout Sep 26 '14

Just adding on, maybe they actually meant === in which case it is actually pretty different. IIRC if variable is 1, variable == true is true in JavaScript. On the other hand, variable === true would be false.

This is why I stick to more statically typed languages.

1

u/IMMENSELY_BRAVE Sep 26 '14

They aren't functionally different

Yes they are.

#include <iostream>
int main()
{
    int i = 123;
    if (i        ) std::cout << "if (i)\n";
    if (i == true) std::cout << "if (i == true)\n";
    return 0;
}

Will only print if (i), but not if (i == true), because true gets converted to 1.

So if you compare stuff to true and change your boolean to an integer, you will most likely get the wrong result.

1

u/Protuhj Sep 26 '14

I meant in the case that variable was a boolean, that it wasn't functionally different.

My example of saying if someone changed the type of variable to an int, if (variable) would be true for all non-zero values, whereas if (variable == true) would show that the original design meant for it to be a boolean, so whoever changed the code needs to be sure they handle that properly.

2

u/IMMENSELY_BRAVE Sep 26 '14

Oops, I misunderstood you then. Carry on.

2

u/ObscureCulturalMeme Sep 26 '14

In Lua, just to give a counter-example, sometimes I really need to know whether a value is an actual boolean false, or is merely nil (which is coerced to false when evaluated in a boolean expression). That's about the only times you'll see me write code like

if foo == false then

The rest of the time, I'm right there with you.

7

u/hatsune_aru Sep 25 '14

not to mention... the function loops through the entire array even if it finds a match. putting !badword in the for loop guardian would make it a little better...

6

u/Seeker_Of_Wisdom Sep 25 '14

Or just put a break in there. But yeah it's not efficient.

2

u/ChrissiQ Sep 25 '14

And that check happens after badword was just set to true. They could have just added the document.location into the previous block.

So much fuck I don't even.

1

u/Tyler11223344 Sep 25 '14

Well I'm not particularly involved in scripting, but that statement's after the for loop ends, so I don't know if it'd cause problems to run that line for every bad word found, but that may be why. Although, if he'd just end the loop as soon as one word was found he wouldn't have to do this in the first place.....

1

u/ChrissiQ Sep 26 '14

That's partly true but the thing is, it will only ever be true once. Because after it's true, you get an alert and get redirected.

1

u/Tyler11223344 Sep 26 '14

Oh ok, I didn't know that's what that line did, thanks.

14

u/mennotron Sep 25 '14

I guess they'll have to start searching for paid porn then

13

u/[deleted] Sep 25 '14

HOW WILL WE FIND PORN NOW???

14

u/program_this Sep 25 '14

"Life finds a way..."

8

u/GrayFox89 Sep 25 '14

Ah, a clbuttic mistake.

7

u/[deleted] Sep 25 '14

Oh shit it even calls FBI.com

5

u/ekolis Sep 26 '14

Had to check if they actually redirected to fbi.com instead of fbi.gov after reading this...

5

u/inconspicuous_male Sep 25 '14

What about websites that ask you to pay for porn?

14

u/scorcher24 Sep 25 '14

What about websites that ask you to pay for porn?

http://i.imgur.com/2HOm3vQ.gif

1

u/[deleted] Sep 25 '14

I get it, this would pop up an alert over and over again for every time this detects a bad word.

8

u/amphetamachine Sep 25 '14

Only until it redirected you to the FBI.

6

u/[deleted] Sep 25 '14

indeed, that is outside of the for loop.

at least it doesn't run for every word in the document - assuming he is populating 'word' correctly.

It bothered me that 'free sex' is considered a word, and not 'words'.

7

u/jervin3 Sep 26 '14

It bothers me that 'free sex' is considered bad.

5

u/ekolis Sep 26 '14

Odd that he put a specific site in there... sounds like they were linking directly to his script on his server, as opposed to using a local copy! Otherwise what's the point of changing the script, unless he planned on them getting it? Or maybe he's just paranoid...

3

u/[deleted] Sep 26 '14

Yep, in which case it would probably work as intended.

1

u/zman0900 Sep 25 '14

WTF is going on with that last for loop? Doesn't look like this thing will even call the function with any data.