Member-only story
Writing Bad Code is Fun
Just Don’t Do This At Work
3 min readOct 7, 2022
To my fellow software engineers — It’s been a long day at work, and you’re probably tired after a whole day of writing proper production-grade code. It’s time to relax, and what better way to unwind is there than writing some nonsensical bad code? Here we go.
1) Double Negatives
score = something.getScore()if not not score >= 90:
print("You get a distinction")
else:
print("no distinction")
Or even:
# a function that adds 2 numbers together
def add(x, y):
return x - (-y)
2) How To Screw Up Your Own Class
class Dog():
def __init__(self, name, age):
self.name = name
self.age = age def __getattribute__(self, attr):
if attr == "__dict__":
return "hehehe"dog = Dog("Rocky", 5)
print(dog.name) # None
print(dog.age) # None
For those of you who aren’t too familiar, the __dict__
attribute stores all other attributes. If we call object.name
, we are actually doing object.__dict__["name"]
.
The __getattribute__
magic method defines what happens when we try to get an attribute from an object…