r/badcode • • Oct 06 '21

sql So umm this is from Twitch source code leak . NSFW

Post image
2.9k Upvotes

400 comments sorted by

View all comments

Show parent comments

8

u/[deleted] Oct 07 '21

I imagine this started with like 10 lines and they just kept adding to it. Now it got to a point where redoing it is just not worth it.

6

u/IAmTheSysGen Oct 07 '21 edited Oct 07 '21

Man, it's pretty easy to read from a file each line in a for loop.

You can also easily refactor this code. Copy the lines to a banlist.txt file.

Do a search and replace "OR ue.login LIKE " with ""

Do a simple

file = open("banlist.txt")
for (line in file):
    if( re.match( ue.login, line )):
        rejectUsername()

Or if you want to be as efficient as the OP

file = open("banlist.txt")
if (re.match("|".join(file.readlines()), ue.login)):
    rejectUsername()

It should take at most 15 minutes to refactor the code above into the proper way (tm).

0

u/d_thinker Oct 07 '21

So opening a file each time user tries to sign up, going through each line of the file that gets bigger every day and doing regex match against each line? OK, I see you didn't write anything at scale.

3

u/IAmTheSysGen Oct 07 '21

You know you can just keep the file open, right? You don't have to open and close it for every user.

You're not matching regex against every line. Read the last code block carefully. You're creating one regex that matches every line in the file simultaneously.

1

u/Lost4468 Oct 07 '21

Redoing it isn't going to change much whether it's 5 lines or 5000 lines? I would just implement a version to load them from a file (or database). Then just copy the 5000 lines into vim, and then do some fancy search and replace, or create a macro. Really it shouldn't take more than a few minutes to change them from this format into a file format, and the actual implementation of loading from a file is going to take the same amount of time regardless of the number (excluding stupidly high numbers of course).