r/badcode • • Apr 24 '21

python very efficient and scalable code

Post image
1.0k Upvotes

110 comments sorted by

View all comments

Show parent comments

2

u/Own-Plantain4057 Apr 25 '21

im very new to coding ok? ;-;

2

u/Suekru Apr 25 '21

Is this actually yours?

2

u/Own-Plantain4057 Apr 25 '21

yeah it is lol

8

u/Steppy20 Apr 25 '21

u/RoboticPlayer has a really good solution in one of the other threads:

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

So where you call those different functions, instead just call this function and give it the name of the tag as a parameter.

For example using choseTag('Comedy') instead of choseTagComedy() would have the same result but it means that you don't have to worry about making new functions to do the same logic you've already made.

As a general rule of thumb, if you're running the same code with subtle differences multiple times you can probably write a more abstract (less specific) function that you can use multiple times.

It helps reduce the length of the code whilst improving flexibility of use and can also help prevent errors made when you copy and paste the same thing and make a slightly incorrect change.

u/Naeio_Galaxy also has a very good explanation here: https://www.reddit.com/r/badcode/comments/mxlgzm/very_efficient_and_scalable_code/gvr16rp?utm_source=share&utm_medium=web2x&context=3