• arrakark@10291998.xyz
    link
    fedilink
    English
    arrow-up
    2
    ·
    edit-2
    6 hours ago

    I straight up never got a nice answer from StackOverflow on this. Say you have 5 classes, each requiring access to the data members/functions of the others. What’s a nice way to solve this problem? I’ve thought of only two nice shit methods:

    • Pass pointers/shared-pointers etc to each class, but not through the constructor but a setter function
    • Pass lambdas or std::function everywhere. Yuck! Still doesn’t put each object in a valid state in the constructor.
    • ☆ Yσɠƚԋσʂ ☆@lemmy.mlOP
      link
      fedilink
      arrow-up
      1
      ·
      2 hours ago

      The. real question is whether this problem needs to be modelled using classes in the first place. The alternative is to just have a set of composable functions that take a piece of data and return a modified version that can be passed to a different function.

    • Kayana@ttrpg.network
      link
      fedilink
      arrow-up
      3
      ·
      5 hours ago

      Well, what problems are you trying to solve by having the classes all access each other’s data members? Why is that necessary?

  • four@lemmy.zip
    link
    fedilink
    English
    arrow-up
    19
    ·
    17 hours ago

    I misread Exceptioncatcher as Exceptionhatcher and I think it still fits

    • xthexder@l.sw0.com
      link
      fedilink
      arrow-up
      3
      ·
      7 hours ago

      I believe the lifecycle goes ExceptionLayer, ExceptionIncubator, ExceptionHatcher

      It’s critical you don’t throw your exceptions too early, they need to learn to fly first 🤣

  • undefined@lemmy.hogru.ch
    link
    fedilink
    English
    arrow-up
    6
    ·
    13 hours ago

    Feeling attacked with Leggable and Fleable. I’ve been known to write a concern or two in Ruby on Rails but what can I say? I like my code DRY.

    • xthexder@l.sw0.com
      link
      fedilink
      arrow-up
      1
      ·
      7 hours ago

      My biggest problem with it is that those aren’t verbs. You might have LegCount -> Countable and FleaCount -> Countable though.

    • ImplyingImplications@lemmy.ca
      link
      fedilink
      arrow-up
      27
      arrow-down
      1
      ·
      17 hours ago

      Functional Programming Theory: 500 pages of lambda calculus and endofunctors

      Functional Programming Practice: Quicksort

      • ☆ Yσɠƚԋσʂ ☆@lemmy.mlOP
        link
        fedilink
        arrow-up
        4
        ·
        16 hours ago

        There’s plenty of real world code written in functional languages. Also, this idea that FP is somehow more complex is complete nonsense. Lisp family are some of the easiest languages to learn.

    • four@lemmy.zip
      link
      fedilink
      English
      arrow-up
      10
      ·
      17 hours ago

      Functional programming is perfect for all my projects that I don’t start!

      • balsoft@lemmy.ml
        link
        fedilink
        English
        arrow-up
        1
        ·
        7 hours ago

        TBH Rust is pretty nice, it borrows (pun intended) a lot of ideas from the functional world (algebraic data types, traits, closures, affine types to an extent, composition over inheritance, and the general vibe of type-driven development), but it’s much easier to write fast, efficient code, integrate with decades of libraries in imperative languages, and the ecosystem somehow feels mature already.

        • ☆ Yσɠƚԋσʂ ☆@lemmy.mlOP
          link
          fedilink
          arrow-up
          1
          ·
          2 hours ago

          Rust solves a specific problem, and it’s good at letting you write correct programs with low resource usage. It’s definitely a huge improvement on C and C++.

          That said, I find a language like Clojure is far more productive because it’s more expressive, and development is done interactively. With Clojure, you start up your program, connect the editor to it, and evaluate code as you go seeing changes live. Once you’ve worked this way, it’s really hard to go back to having to compile your whole program each time you want to see what it’s doing. It’s like having a conversation with the compiler. It makes it very easy to experiment with different ways to solve a problem, and it gives a lot of confidence because you always see exactly what the code is doing. Clojure also interops with JVM and Js runtimes, so those entire ecosystems are available for use.

          Incidentally, there’s a Lisp style language that embraces a lot of Rust principles. https://github.com/carp-lang/Carp

  • TimeSquirrel@kbin.melroy.org
    link
    fedilink
    arrow-up
    5
    ·
    16 hours ago

    I’m just a hobbyist but…are you guys using exceptions like they’re conditional statements?? I thought those were for only when shit is seriously wrong and execution can’t continue in the current state. Like if some resource was in a bad state or some input was malformed.

    Or maybe I haven’t worked on anything complex enough, I dunno.

    • sbv@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      8
      ·
      14 hours ago

      I thought those were for only when shit is seriously wrong and execution can’t continue in the current state.

      That’s how it starts. Nice and simple. Everyone understands.

      Until

      some resource was in a bad state

      and you decide you want to recover from that situation, but you don’t want to refactor all your code.

      Suddenly, catching exceptions and rerunning seems like a good idea. With that normalized, you wonder what else you can recover from.

      Then you head down the rabbit hole of recovering from different things at different times with different types of exception.

      Then it turns into confusing flow control.

      The whole Result<ReturnValue,Error> thing from Rust is a nice alternative.

    • ☆ Yσɠƚԋσʂ ☆@lemmy.mlOP
      link
      fedilink
      arrow-up
      8
      ·
      16 hours ago

      As a rule, exceptions should indeed be used for behaviors that are outside normal execution flow. For example, you might throw an exception if a file you’re trying to access doesn’t exist, or a network call fails, etc.

    • tunetardis@lemmy.ca
      link
      fedilink
      English
      arrow-up
      3
      ·
      14 hours ago

      I suppose it depends on the language? For the most part I think you’re right. Exceptions are only used (if at all) in situations where a program diverges unexpectedly from its normal flow. But take a language like Python. They’re just everywhere. Even your plain old for loop ends on an exception, and that’s just business as usual.

    • Ephera@lemmy.ml
      link
      fedilink
      arrow-up
      1
      ·
      12 hours ago

      You don’t want to use exceptions in normal control flow, because they’re extremely slow. Every time you throw an exception, it has to collect a stacktrace, which is hundreds, if not thousands, of calculations, compared to a handful of calculations for returning a boolean or an enum variant.

    • Odinkirk@lemmygrad.ml
      link
      fedilink
      arrow-up
      1
      ·
      14 hours ago

      You can set up a global exception handler in some frameworks. By having multiple (not a crazy amount) of exceptions, you can set up logic for how to handle that kind of error. Then you can just throw the exception instead of writing individual catch blocks.

      This is especially helpful in things like a REST API where user input can cause all kinds of fun, let alone network issues, problems with your data source, etc.