• jtrek@startrek.website
    link
    fedilink
    arrow-up
    47
    arrow-down
    1
    ·
    1 day ago

    I use keyword arguments in Python to minimize this pain. Instead of

    
    create_user("Bob", True, False)
    

    it’s

    
    create_user(name="Bob", admin=True, send_email=False)
    

    JavaScript makes that more cumbersome with the object thing , but it’s better than nothing.

    • Eager Eagle@lemmy.world
      link
      fedilink
      English
      arrow-up
      20
      ·
      edit-2
      1 day ago

      This! There’s also a longstanding open issue on rust to allow a similar way to pass keyword/named arguments. IMO every modern language should have something like this. Makes all the difference when reading code.

      Inlay hints from your editor are a good help, but this should be built into the language.

      • calcopiritus@lemmy.world
        link
        fedilink
        arrow-up
        7
        ·
        9 hours ago

        Not saying that named parameters are bad, but the builder pattern serves the same purpose and imo it’s more ergonomic.

        TrainBuilder::new()
          .with_electric_motor(true)
          .with_width(1.0)
          .build()
        

        I don’t care about the color of the train or the amount of wheels, I just want the default train with a few changed parameters.

        If you do named parameters in rust, you would need to set every parameter. The only way to not set every parameter is to give a special meaning to the Default trait. But that is uncommon to happen in rust. And many structs that could easily derive Default don’t.

        • Eager Eagle@lemmy.world
          link
          fedilink
          English
          arrow-up
          1
          ·
          2 hours ago

          If you do named parameters in rust, you would need to set every parameter.

          If that’s the condition, yeah, it’s bad. The good thing about how Python handles it is that they can still be mandatory or optional. And that makes it a more flexible option than the builder pattern because it applies to any function or method.

      • FizzyOrange@programming.dev
        link
        fedilink
        arrow-up
        10
        arrow-down
        1
        ·
        22 hours ago

        Rust doesn’t need this as much because it has enums so you can just do create_user(user, Role::Admin, Notify::None).

        • Eager Eagle@lemmy.world
          link
          fedilink
          English
          arrow-up
          9
          ·
          21 hours ago

          you can have a better data structure in any language, but rarely someone will bother doing that for booleans

          • FizzyOrange@programming.dev
            link
            fedilink
            arrow-up
            1
            ·
            43 minutes ago

            Yeah I do wonder if we need an easier way to declare these things because programmers are lazy and even in Rust I wouldn’t always bother.

            You can kind of do it in Typescript with strings:

            function create_user(role: "admin" | "normal")
            

            But of course the downside is they are strings at runtime. I’m sure it’s possible though.

          • Traister101@lemmy.today
            link
            fedilink
            arrow-up
            3
            ·
            18 hours ago

            Right cause the boolean isn’t a named type. If you have two possible states that can be represented with a boolean, or an enum of the two possible states which embeds more info into the callsite

    • ugo@feddit.it
      link
      fedilink
      arrow-up
      5
      ·
      24 hours ago

      zig uses anonymous structs for the same effect

      doWork(.{
          .flurbify = true,
          .flurbification_intensity = 1001,
      });
      
    • Riskable@programming.dev
      link
      fedilink
      English
      arrow-up
      3
      ·
      1 day ago

      Thank you for posting this comment. I came here to write the exact same thing and now I don’t have to!

      👍