4 ms·
To me this seems like a solution in search of a problem. Checking for "None" is not that bad and doesn't require nested "if" statements if you apply this patter
by AlexandrB 4y ago
To me this seems like a solution in search of a problem. Checking for "None" is not that bad and doesn't require nested "if" statements if you apply this pattern instead:
if user is None:
return
balance = user.get_balance()
if balance is None:
return
credit = balance.credit_amount()
# etc...
Mypy also correctly detects that user/balance/etc. can't be null after each check. Of course it's better to design functions/methods that never return "None", but that's not always an option. This approach also has the advantage that you can easily log at what stage the sequence of method calls failed. Stuffing this into a bunch of lambdas makes that much harder.
- pphysch 4y agoImperative styles like yours are readable, and more importantly, hackable. Business processes are rarely set in stone and the above code can be easily modified, as opposed to a "beautiful" "pure" "correct" ball of functional yarn that must be carefully unwound and rearchitected.
- _v7gu 4y agoYea, because the following equivalent piece of code is much more difficult to read without returns after every statement user |> mmap(get_balance) |> mmap(get_credit_amount)