News for hackers. Stuff that matters.
Re:Yes-rs: A fast, memory-safe rewrite of the classic Unix y... by johnisgood on Tuesday May 27, 2025 @09:40AM (#44106911)
How about:

  for (;;) {
    if (argc > 1)
      puts(argv[1]);
    else
      puts("y");
  }
?

You said "it’s about duplicating logic that should inherently be the same", but that is exactly how it is more explicit, by having this duplication. I assume your problem is with the two "puts()"?

Re:Yes-rs: A fast, memory-safe rewrite of the classic Unix y... by williamdclt on Tuesday May 27, 2025 @11:58AM (#44108192)
That's a whole lot less efficient than both other options, the condition will be checked at every loop, that's not cheap
Re:Yes-rs: A fast, memory-safe rewrite of the classic Unix y... by johnisgood on Tuesday May 27, 2025 @12:06PM (#44108273)
Oh, I know. :P
Re:Yes-rs: A fast, memory-safe rewrite of the classic Unix y... by layer8 on Tuesday May 27, 2025 @10:01AM (#44107123)
Your proposal is a bit better than the original, although it still duplicates the puts (imagine a variant where you’d want to handle I/O errors), and some will be bothered by the fact that the same unchanging condition is being retested in each loop iteration (the compiler may even warn about it).

But still, I don’t see why you wouldn’t first name what you want to output before starting the outputting. If anything, I’d place the whole output loop in a separate function and have two calls to that function. Nevertheless, it’s even better to express in code the fact that the program doesn’t want to make a distinction between a literal “y” and an argument “y”, by consolidating them into the same variable.

Another way to do this would be to have a static default argument array containing the “y”, and for example having:

    if (argc <= 1) { argv = default_argv; }
    for (;;) { puts(argv[1]); }
This would make explicit the fact thst the argument-less invocation is merely a shortcut for an invocation with an argument and doesn’t otherwise provide any new or different behavior.

Though I think the separate variable (what) is clearly preferable.

Re:Yes-rs: A fast, memory-safe rewrite of the classic Unix y... by johnisgood on Tuesday May 27, 2025 @10:16AM (#44107247)
I have made the decision to use your "what" method many times before, but in this particular case I do not see the reason to do that, and perhaps this is what I have an issue with. There are many cases in which I would definitely use "what".