response = server.Call(request)
if response.GetStatus() == RPC.OK:
if response.GetAuthorizedUser():
if response.GetEnc() == 'utf-8':
if response.GetRows():
vals = [ParseRow(r) for r in
response.GetRows()]
avg = sum(vals) / len(vals)
return avg, vals
else:
raise EmptyError()
else:
raise AuthError('unauthorized')
else:
raise ValueError('wrong encoding')
else:
raise RpcError(response.GetStatus())
|
response = server.Call(request)
if response.GetStatus() != RPC.OK:
raise RpcError(response.GetStatus())
if not response.GetAuthorizedUser():
raise ValueError('wrong encoding')
if response.GetEnc() != 'utf-8':
raise AuthError('unauthorized')
if not response.GetRows():
raise EmptyError()
vals = [ParseRow(r) for r in
response.GetRows()]
avg = sum(vals) / len(vals)
return avg, vals
|
Thanks for the article. I like the example. As a rule of thumb I'd add replace conditional with polymorphism if possible.
ReplyDeleteAren't the following 2 logical checks flipped on the exceptions they should raise?
ReplyDeleteif not response.GetAuthorizedUser():
•• raise ValueError('wrong encoding')
if response.GetEnc() != 'utf-8':
•• raise AuthError('unauthorized')
Regarding my logic flip comment lol. Disregard since I clearly see it was intentional after continuing with the artcile
ReplyDeleteI recently refactored a too much nested logic check because I had the very impression it wasn't redeable. Glad to see that kind of article, I will keep this tip in mind.
ReplyDeleteThe one difference I would have is if error0 else if errorN else { success }.
ReplyDeleteSlightly easier to immediately go, ah huh, error(s) then success paths. Thanks to less whitespace + usage of else keyword.