r/badcode • • Oct 19 '16

js /pol/'s mass voting script

Post image
4 Upvotes

5 comments sorted by

-6

u/witchwind Oct 19 '16

For those of you who haven't realized why this is bad code yet, it passes strings into setTimeout() and defines global variables.

14

u/kurosaur Oct 20 '16

In such small scripts, the use of global variables is trivial and not a great concern. In some instances, such as when using OpenGL and related libraries, global variables are the only way to pass information around the program, due to the presence of callback functions and other things that inhibit or outright prevent parameter passing.

Globals can be a source of confusion and bugs, but when the entire source code file can be viewed at once, they're hardly worth the criticism.

5

u/witchwind Oct 20 '16

The script also makes recursive calls to setTimeout indefinitely (lol), even after everything's been voted on.

2

u/kurosaur Oct 24 '16

That is admittedly bad code, but I'm willing to bet that the script would run out of posts to downvote before the recursion nesting got too deep.

2

u/witchwind Oct 24 '16

jQuery returns an empty jQuery collection when the selector doesn't match any elements. The stack won't overflow because setTimeout is asynchronous, but the browser will keep making useless jQuery calls until the user navigates to another page.