[Tutor] extracting information from a complex dict.

David Rock david at graniteweb.com
Sun Jan 31 12:38:12 EST 2021


* Cameron Simpson <cs at cskk.id.au> [2021-01-31 17:54]:
> 
> - otherwise something unexpected! raise an exception!
> 
>     else:
>         raise RuntimeError(
>             "at keys %r got an unexpected type %s:%r"
>             % (previous_keys, type(subitem), subitem))
> 
> Plenty of people wouldn't bother with the exception, but it is good 
> defensive programming: handle the known cases, then explode with a 
> informative exception if something is somehow unhandled. Much better 
> than having the unhandled type silently ignored.

I can't agree with this more.  Just this week I inherited some code at work
where absolutely no regard for handling exceptions was done, and they all
silently get ignored, causing all kinds of unseen issues.  Using an if
statement without planning for the else condition leaves no protection for new
data added later that does not conform to the original logic.  You will never
know something is wrong unless it actually breaks something later.  Things that
"work incorrectly" are insidious because they hide in plain sight.

-- 
David Rock
david at graniteweb.com


More information about the Tutor mailing list