The other comments already answers the question, but in case you don't really understand what they say, here's my answer.
In short, it is not maintainable and it is long. It is not hideous, but as an experienced developer you think "what a derp" or "it should have been painful to write this code".
Here the biggest problem IMO is maintainability, but the most chocking thing is the length.
maintainable -> easy to modify, to maintain. If I want to add a history.append("Comedy") I'll have to add it 7 times, which is way longer than add it only once.
It can be reduced from 42 lines of code to 6, with a function of this kind :
def chosetag(tag):
if tag in selectedtags:
selectedtags.remove(tag)
else:
selectedtags.append(tag)
print(selectedtags)
They might have already though of doing that, but I guess they didn't because they needed to pass in a function later on. Lemme explain :
They are using tkinter that allows to do a graphical user interface (i.e. a window where you can click and see what happens), and probably use buttons. To determine what a button does, tkinter allows you to pass in a function that will be called when the button will be pressed. So they needed a function per button. Thus having 7 function that does almost the same things.
But in python there is lambda functions, that allows to write a very small function anywhere. For instance, one can do :
x = lambda: chosetag("hello")
And so, x becomes a function whose body is chosetag("hello") so whenever one does x() it calls chosetag with the argument "hello".
That allows to write less code, and if you want to change the function (that is likely to happen on useful projects usually) there's only one function to change.
In case there is no lambda function, there is still the possibility of doing this :
Plus, in python like in many other languages, you can create a function inside an other one. So there is the following option that does not require any lambda :
def tagChooseMaker(tag):
def chosetag():
if 'Comedy' in selectedtags:
selectedtags.remove(tag)
else:
selectedtags.append(tag)
print(selectedtags)
return chosetag
Instead, when you call tagChooseMaker(tag), it crates and returns a fresh new chosetag for the tag given in parameter. And so, you can then do chosetagComedy = tagChooseMaker("Comedy"). This last option is less readable than the other ones tho, but maybe a tiny bit faster (but readability is more important in this case).
So yeah, there was many ways to make this code better for maintenance and readability (it's easier to read a short code than a long one), but they didn't know them. And that's fine as long as this allows them to learn not to do that. This is one of the many moments where one understand that in dev, there are ways of factoring code, and whenever two different things do almost the same thing, there is very often a way to put everything in common.
This is my friends code. I improved it by writing a function similar to your choosetagmaker function. I didn't think of lambdas though! Thanks for the insightful comment
6
u/weezkhan Apr 24 '21
Could someone explain to me what's wrong, am a newbie in programming