[HN Gopher] Raising exceptions or returning error objects in Python
       ___________________________________________________________________
        
       Raising exceptions or returning error objects in Python
        
       Author : EntICOnc
       Score  : 10 points
       Date   : 2022-06-09 06:40 UTC (16 hours ago)
        
 (HTM) web link (lukeplant.me.uk)
 (TXT) w3m dump (lukeplant.me.uk)
        
       | Zababa wrote:
       | Interesting, I've seen the same discussion but applied to OCaml
       | and with an option type vs an exception. For example, the module
       | List has both List.nth that raises Failure if the list is too
       | short, and List.nth_opt that returns None of the list is too
       | short. One aspect that I've seen mentioned about OCaml and not
       | with this article is performance. An option will trigger an
       | allocation, and will generay be slower than an exception. One the
       | other hand, the option approach is safer. I'm not sure if that's
       | true with Python too though.
        
         | g_delgado14 wrote:
         | Performance cost is a legitimate concern but one that should be
         | considered in relative terms.
         | 
         | e.g. yes there are more allocations, but in what context is the
         | code being used? Are we building gigabyte-scale real-time
         | systems? no? ok then let's maybe not worry about performance
         | right now and instead optimize for intuition, maintainability
         | and readability. Obviously while at the same time monitoring
         | CPU and memory consumption of our applications to help us
         | determine when we might want to look to optimize our code.
        
       | g_delgado14 wrote:
       | Yessss... I'm a big advocate of using error objects rather than
       | throwing exceptions. I think the industry over-uses throwing
       | exceptions and it's basically turned into the new GOTO statement.
       | 
       | I wrote an article about this as well but for TypeScript [0] and
       | I've got a relatively popular npm package that does what I
       | mention in the article [1]
       | 
       | ---
       | 
       | [0] - https://gdelgado.ca/type-safe-error-handling-in-
       | typescript.h...
       | 
       | [1] - https://www.npmjs.com/package/neverthrow
        
       | hlpweg wrote:
       | Python front page lecture #671729.
        
       ___________________________________________________________________
       (page generated 2022-06-09 23:00 UTC)