• aubeynarf@lemmynsfw.com
    link
    fedilink
    arrow-up
    20
    arrow-down
    1
    ·
    2 months ago

    You have to explicitly check if the return value is an error and propagate it. You write the same boilerplate if (err) return err over and over again, which just litters your code.

    That’s only true in crappy languages that have no concept of async workflows, monads, effects systems, etc.

    Sad to see that an intentionally weak/limited language like Go is now the counterargument for good modeling of errors.

    • sping@lemmy.sdf.org
      link
      fedilink
      English
      arrow-up
      15
      ·
      2 months ago

      I naively thought it I may as well take a job using Go, as learning a new language is broadening, and some people like it, so lets find out first hand… I knew it was a questionable choice, looking at how Go adoption tailed off a while ago.

      Turns out I hate Go. Sure it’s better than C but that’s a very low bar, and C was never a good alternative choice for the use cases I’m encountering. I’m probably suffering from a codebase of bad Go, but holy shit it’s painful. So much silent propagation of errors up the stack so you never know where the origin of the error was. So very much boilerplate to expand simple activities into long unreadable functions. Various Go problems I’ve hit can be ameliorated if you “don’t do it like that”, but in the real world people “do it like that” all the time.

      I’m really starting to feel like there are a lot of people in the company I’ve joined who like to keep their world obtuse and convoluted for job security.

    • matcha_addict@lemy.lol
      link
      fedilink
      English
      arrow-up
      4
      ·
      2 months ago

      Can you please demonstrate how async workflows and monads resolve this issue?

      Wouldn’t effect systems still be considered exceptions, but handled differently?

      • lad@programming.dev
        link
        fedilink
        English
        arrow-up
        9
        ·
        2 months ago

        I don’t know the answer to your question, but I think that what is needed is just a bit of syntactic sugar, e.g. Rust has ? for returning compatible errors without looking into them. That seems to be powered by Try trait, that may be a monad, but I am not fluent enough to check if it formally is.

      • baseless_discourse@mander.xyz
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        2 months ago

        In Maybe monadic, its monadic bind will automatically resolves any failed computation, and don’t need explicit checking.

        for example, the code in Haskell looks something like the following:

        fib: Int -> Int -> Maybe Int
        fib max_depth idx =
          do
             guard (0 <= max_depth)
             n1 <- fib (max_depth - 1) (idx - 1)
             n2 <- fib (max_depth - 1) (idx - 2)
             return (n1 + n2)
        

        Haskell type class system automatically figures out this is a maybe monad, and check for error accordingly.

        Notice, unlike the C code the author provide, this haskell code will exit immediately when n1 failed and never compute n2, similar to the behavior of the exception code. Thus I believe his point about performance is at least unjustified, if not wrong.

        Another interesting fact about this code is that there is nothing that is built into the compiler/interpretor (except the do expression, which is just a minor syntactical sugar). For this code, the compiler designers don’t need to design special semantics for raise and catch. Everything here, guard, return, and the Maybe monad (which is in charge of propagating errors) is defined by the user, using normal functions, no metaprogramming involved.

        Wouldn’t effect systems still be considered exceptions, but handled differently?

        Yes, unlike monad, the error in algebraic effect is propagated by the compiler/interpretor, instead of user defined. But unlike implicit effect, explicit effect (algebraic effect, throwable, etc.) makes it clear how the code can go wrong.

        Although explicit error through monad or algebraic effect is more clear in general, there are special cases where explicit effect is undesirable. One such example is effect pollution: low-level effects that are unlikely to cause impure behaviors are unnecessarily propagated through the call stack. This problem can make the code more verbose and difficult to handle.

    • lysdexic@programming.dev
      link
      fedilink
      English
      arrow-up
      3
      ·
      2 months ago

      That’s only true in crappy languages that have no concept of async workflows, monads, effects systems, etc.

      You don’t even need to sit on your ass and wait for these data types to be added to standard libraries. There are countless libraries that support those, and even if that is somehow not an option it’s trivial to roll your own.