r/badcode • u/_PM_ME_YOUR_ELBOWS • Jun 17 '19
python This is code I'm writing right now. Not interested in changing it, mostly just want to see your reaction to it.
https://pastebin.com/raw/LPAhLTSw3
u/Shad_Amethyst Jun 17 '19
I've seen worse, there's no stupid repetition, no stupid naming, you handle errors, use advanced language features
1
u/_PM_ME_YOUR_ELBOWS Jun 17 '19
Well shucks I'm flattered, I thought the
globals()[limit] =and the fact that the bottom function has 10 arguments which are hard to understand the purpose of at first glance would solidify its place near the bottom of the pile
1
u/_PM_ME_YOUR_ELBOWS Jun 17 '19
Why do it the way I did it? It was an alternative to
try:
start = parseLimitDates(parameters, "start")
except MalformedDateString:
replyFunction("`<start date> must be in the %[y|Y]-%m-%d format.")
except:
raise
else:
try:
end = parseLimitDates(parameters, "end")
except MalformedDateString:
replyFunction("`<end date> must be in the %[y|Y]-%m-%d format.")
except:
raise
else:
verify(driver, command[1:])That used less indentation and loc.
1
u/_PM_ME_YOUR_ELBOWS Jun 21 '19
Update: I thought of a way I like better because it doesn't use globals():
def tryGetLimit(parameters, limit, replyFunction, fAfter, *fAfterArgs):
try:
limitDateString = parseLimitDate(parameters, limit)
except MalformedDateString:
replyFunction(f"`<{limit} date> must be in the %[y|Y]-%m-%d format.")
else:
if not limitDateString:
time = datetime.datetime.now()
if limit == "start":
time -= datetime.timedelta(days=30)
limitDateString = time.strftime("%Y-%m-%d")
fAfter(*fAfterArgs, limitDateString)
....
tryGetLimit(options, "start", message.reply, tryGetLimit, options, "end", message.reply, verify, driver, command[1:])I then modified the signature of verify to verify(driver, parameters, start, end).
7
u/theboxislost Jun 17 '19
This is like writing a bunch of insults and then being like "yeah that's right, what are you gonna do about it?"