I keep tripping over "true, false, true"

(allthingssmitty.com)

17 points | by AllThingsSmitty 1 hour ago

17 comments

  • dematz 15 minutes ago
    First, sentences like "Not because it’s complicated. Just because I have no idea what I’m looking at." and "Tiny interruption. Still annoying every time." fatigue me, it's like you have an editor who, no matter what the content is, tries to spice up your writing with lots of little punchy exclamations, not everything needs such emphasis

    Second, this may differ a bit from language to language, but maybe those booleans should not be a boolean: https://gleam.run/documentation/conventions-patterns-and-ant... for example isAdmin boolean could instead be a UserRole custom type, with variants Normal and Admin, which is easier to understand in the function call, and extendable with another Moderator (or whatever) variant

    • megiddo 12 minutes ago
      Exactly my thought. I know languages differ in support ... but enum is right there.
  • NooneAtAll3 14 minutes ago
    > toggleMenu(true); That’s clear enough. the meaning is obvious

    so... it does toggle the menu? and toggleMenu(false) doesn't toggle it and keeps it as it is?

    or is it toggle extended menu vs toggle basic menu?

  • tantalor 18 minutes ago
    This one is a classic,

    Avoid the Long Parameter List

    https://testing.googleblog.com/2024/05/avoid-long-parameter-...

  • disillusionist 11 minutes ago
    I've been using this pattern for the past couple years for the benefits the author mentions. In addition to that, it can help with overly complicated functions (which, ok, could probably be refactored) that have multiple optional arguments.
  • edwcross 24 minutes ago
    OCaml has had labeled arguments for decades, so I assumed other languages would have added something similar by now. In C-style, it would be like:

      createUser(user, ~isAdmin:true, ~sendWelcomeEmail:false)
    
    Even though in OCaml's functional style it is actually like this:

      createUser user ~isAdmin:true ~sendWelcomeEmail:false
    
    Using the fact that a variable named exactly like a labeled argument is automatically assigned to it, we can make the call more concise (especially if reusing existing variables):

      let isAdmin = true in
      let sendWelcomeEmail = false in
      createUser user ~isAdmin ~sendWelcomeEmail
    • MathMonkeyMan 12 minutes ago
      You can do this as a convention in javascript since 2015, but I haven't seen a library that does it:

          > function foo({a, b, c}) {
          ... return {x: a, y: b, z: c};
          ... }
          undefined
          > foo({a: 1, b: 2, c: 3})
          { x: 1, y: 2, z: 3 }
          > const a = 'A', b = 'B', c = 'C';
          undefined
          > foo({a, b, c})
          { x: 'A', y: 'B', z: 'C' }
          >
    • lostmsu 20 minutes ago
      C# has that
  • kevsim 44 minutes ago
    This is commonly referred to as "the boolean trap". You'll find lots of articles about it.
  • PaulKeeble 1 hour ago
    There is something to be said for the bitmasks that are so common in C, createUser(user, ADMIN | SENDMAIL); has a lot more clarity than createUser(user, true, false, true);

    I don't mind the object approach used here but its quite verbose in comparison even in Javascript. Having to name the variable and set whether its true or false is a lot more than needs to be done. Booleans in general have quite poor readibility and maintenance especially if a third possibility arrives.

  • tyleo 1 hour ago
    In the last couple of years I’ve started using named parameters a bunch more across languages. I consider objects like this close to the JS version of a named parameter. I probably would have thrown “name” in myself so it’s one arg for the whole func.

    I feel like a goal with good code is localizing understanding even if it occasionally duplicates something like a parameter name.

  • bradrn 1 hour ago
    Also known as ‘boolean blindness‘: e.g. https://cs-syd.eu/posts/2016-07-24-overcoming-boolean-blindn...
  • heyitsdaad 37 minutes ago
    You answered your own question. Call with

    const isAdmin = true; . . . createUser(user, isAdmin, sendWelcomeEmail)

    • rcxdude 17 minutes ago
      If you swap the order of isAdmin and sendWelcomeEmail you'll get no error from the compiler but now the names will be masking the actual behaviour.
  • trgn 1 hour ago
    named arguments are hacking object literals to provide additional readability. it's ok, but not for all code paths, they have a true overhead. problem is that these things start to become idee fixes in teams (all funcs should have named args!). ideally, this could be fixed in the language.
    • tyleo 58 minutes ago
      Agree that it would be nice to fix in the language. It seems like something that even a transpiler could take care of.

      Ultimately I think I’d bias towards readability vs the marginal perf increase though.

  • rideontime 35 minutes ago
    I was nodding along with the piece in the first half, then it repeated the same point five more times and I started to smell slop.
    • empath75 34 minutes ago
      You can tell it was written by claude after just a few sentences, really.
  • esafak 23 minutes ago
    Usable languages let you use named arguments foo(bar=zaz), and linters let you enforce their use for booleans.

    https://kotlinlang.org/docs/functions.html#named-arguments

  • bcjdjsndon 35 minutes ago
    Isn't this more an issue with typescript? Doesn't your ide give you the declaration if you hover over the call?

    > And I’ve seen real calls like this in production code: > updateSettings(user, true, false, true, false)

    Really? He wants named parameters on all function calls cos he's got a memory like a sieve? This is a long solved problem to me

  • readthenotes1 26 minutes ago
    Someone want to recreate Smalltalk...
  • uberman 1 hour ago
    Perhaps, jsdocs might help here.