r/badcode • • Apr 24 '21

python very efficient and scalable code

Post image
1.0k Upvotes

110 comments sorted by

View all comments

101

u/Big16th Apr 24 '21

I wish I knew how to code

284

u/Blackbird-ce Apr 24 '21

Just like the author of this code does

2

u/Own-Plantain4057 Apr 25 '21

im very new to coding ok? ;-;

3

u/Naeio_Galaxy Apr 25 '21

Yeah I understand don't worry, many of us have gone though this. What is important is to learn how to improve, learn what one should not do before doing such things in a released app. It may seem harsh at first, but really we don't hate you at all. And it's not like if it was a code on production, that might be widely used.

So it's alright. We make a bit of fun of this code and help you learn how to avoid it, and that's it 😉

2

u/Own-Plantain4057 Apr 25 '21

thanks man, dont worry i wasn't actually upset; i even provided the screenshot to my friend but this is still nice message :)

1

u/Naeio_Galaxy Apr 26 '21

Oh ok lol ! Yeah no pb ;)

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

1

u/Suekru Apr 25 '21

As others have mentioned, just need to simplify it into one method. But other than that, I wouldn’t call it bad. Just a bit redundant is all.