r/badcode • • Apr 24 '21

python very efficient and scalable code

Post image
1.0k Upvotes

110 comments sorted by

View all comments

-51

u/NetherFX Apr 24 '21

Still pretty readable, and I can't quickly think of an improvement

96

u/RoboticPlayer Apr 24 '21

Just pass it as a parameter

def chooseTag(tag):
    if tag in selectedTags:
        selectedTags.remove(tag)
    else:
        selectedTags.append(tag)
    print(selectedTags)

6

u/lizard450 Apr 24 '21

Use a hashtable would be an improvement... Or an enum bit array.

5

u/RoboticPlayer Apr 24 '21

True, but what I did is simple and it's what I thought of off the top of my head :)

2

u/lizard450 Apr 24 '21

That's how you get to good code. A series of simple improvements.

1

u/AutoModerator Apr 24 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/RoboticPlayer, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

5

u/Schlagbaum3000 Apr 24 '21

Maybe just one function taking a string? You can then create necessary functions with something like. You might then also create a list of functions from a list of possible tags.

chosetagDrama = lambda: chosetag('Drama')

Possibly move the selectedtags into a function parameter, too, to not rely on global variables.

2

u/singletonking Apr 24 '21

Don’t worry, it took me a while to figure out as well

2

u/SuperPotterCrazy01 Apr 24 '21

Wouldn't a set give the same effect?

6

u/Schlagbaum3000 Apr 24 '21

No, each function removes the element if it exists in the list, a set would just do nothing (keep it in the set). This is probably to toggle the selection of tags.

6

u/66bananasandagrape Apr 24 '21

Why not do "if it's in the set, remove it, otherwise add it"?

If you're feeling slick you might even write it as

my_set = set()
...
my_set ^= {"thing_to_toggle"}

Maybe this person wanted to rely on the order of the tags though, in which case using a list is more understandable.

2

u/SuperPotterCrazy01 Apr 24 '21

Ah sorry I didn't see that part