News for hackers. Stuff that matters.
Re:We need to talk about parentheses by xmcqdpt2 on Monday February 12, 2024 @07:31AM (#39344083)
> And this is, in fact, a made-up example but the thing is, I was a C programmer for the past 5 years, and I was working with low-level code (bare metal actually). And I have never seen the use of explicit scoping of variables. [...] I find this a major problem when dealing with C because I always see about 10 uninitialized variables at the start of each function, where half of those are used only once. Somewhere. And thank god, if there are no global variables.

Sure, if you write C like it's 1995, it's not going to be very readable. You don't need to define variable at the beginning of a scope, and you can use {} to scope variables to tighter blocks. And functions can be small.

(Sometimes you need to keep variables for longer than you would like for cleanup reason, that's true.)

Re:We need to talk about parentheses by lex-lightning on Monday February 12, 2024 @07:49AM (#39344209)
The drawback of the microscopic function craze is you lose the ability to read a whole procedure from top to bottom. If some code is truly reused, break it out into a function with a good name, sure.

But in my domain I have seen countless one-line functions used in one place each, and it’s frustrating having to jump around the code (and add to my mental context stack*) rather than just putting some curly braces and a comment. Functions aren’t primarily meant as a hygiene mechanism. I really like the curly brace scoping in Rust.

I found my style partly by reading John Carmack and Casey Muratori’s coding philosophies and putting them to the test for myself.

* For a natural language analogy, consider the difference between parenthetical remarks and asterisks. You have to remember your place in the text.

Re:We need to talk about parentheses by jolt42 on Monday February 12, 2024 @11:59AM (#39347248)
I define functions as often in let/letfn blocks than I do as separate functions, so they are local and in context. Makes it easy to read.
Re:We need to talk about parentheses by lex-lightning on Monday February 12, 2024 @12:28PM (#39347663)
This sounds like another option while maintaining agreement with the gist of what I meant. Those are good, though that’s where I start preferring smallness
Re:We need to talk about parentheses by agentultra on Monday February 12, 2024 @01:46PM (#39348671)
It's a personal preference based on how we mentally model problems. I cannot stand long procedural code. The mental gymnastics required to know what it does is too much. More than 5-6 variables in scope and I have no idea if it does what you say it does. It would take me some time to figure that out. If it is under test we can at least say it's not wrong some times.

I much prefer breaking problems down to the level where I can reason in terms of sets, algebras, and that sort of thing. It's much easier for my brain to rely on properties and the ability to substitute terms in equations than it is to reconstruct a giant state machine and execute it in my head. Much more so when we add concurrency to the mix.

At the end of the day though I don't think there's, "one true way."

Re:We need to talk about parentheses by lex-lightning on Monday February 12, 2024 @02:24PM (#39349105)
When I was a kid I would tell my parents I cleaned my room, then they’d look in the closet and see everything in a pile.
Re:We need to talk about parentheses by pimlottc on Monday February 12, 2024 @05:27PM (#39351382)
Don't let perfect be the enemy of good
Re:We need to talk about parentheses by lex-lightning on Monday February 12, 2024 @07:17PM (#39352534)
I still don’t fold clothes. I just buy stuff that doesn’t wrinkle. Work smarter not harder.
Re:We need to talk about parentheses by agentultra on Tuesday February 13, 2024 @10:50AM (#39358722)
My kid does this. We wanted to find a book. It required us to search through the mountain hidden in the closet and was very annoying. If it was organized we would have known where to find the book.
Re:We need to talk about parentheses by nyrikki on Monday February 12, 2024 @01:52PM (#39348735)
Mostly a problem with people who borrow their concepts of cohesion from MVC, but yes people tend to fundamentally misunderstand SIP and it results in over modularity.

IMHO that is a side effect of how we teach people how to program mixed in with orgs cargo culting acronyms and not the core concepts of ideas.

Re:We need to talk about parentheses by lex-lightning on Monday February 12, 2024 @02:26PM (#39349142)
For real though. This was my biggest frustration when I was an SE.

Want people in a large organization to ignore your ideas? Speak them out loud.

Want them to follow them as dogma? Write a blog post under a pseudonym.

Re:We need to talk about parentheses by mkehrt on Monday February 12, 2024 @03:07PM (#39349729)
You should be able to read a function from top to bottom and it should show you the whole procedure at a given level of abstraction. Hiding the details is the heart of what programming is.

I always try to write code like

fn foo() {

    let foo = do_first_thing();

    let bar = do_second_thing();

    do_third_thing(foo, bar);
}

whenever possible. Then you can see the abstract overview of what the code does at a glance, but you can drill down into the details if you need to.

Re:We need to talk about parentheses by xmcqdpt2 on Monday February 12, 2024 @06:21PM (#39351952)
I dislike microscopic functions a lot too and yet I've never needed 10 uninitialized variables at the beginning of a function. I've written readable 100 lines long functions. I just assumed that the OP was dealing with like 5000 lines long files with statics all over the place.
Re:We need to talk about parentheses by epgui on Wednesday February 14, 2024 @03:46AM (#39367854)
If your functions are 100 lines long, readability may be in the eyes of the beholder. That's how big I'd expect most modules to be.
Re:We need to talk about parentheses by lmm on Monday February 12, 2024 @07:36PM (#39352695)
> you lose the ability to read a whole procedure from top to bottom

An ability that is only valuable if it really is a procedure, some sequence of operations, rather than a function, some forming of a value. I'd agree that procedures should generally not be split up into subprocedures; a procedure has to be read from top to bottom, so factoring out subprocedures only makes it harder to understand. But a function can be understood without understanding the smaller functions that it calls, so breaking functions up into smaller functions - and breaking small functions out of procedures, where possible - makes it easier to understand.

Having a language that makes an explicit distinction between procedures and functions - in particular, where calling a procedure looks different from calling a function - helps a lot (e.g. Haskell).

Re:We need to talk about parentheses by ctrw on Monday February 12, 2024 @07:35AM (#39344107)
So your solution to having too many () is to have just as many {}. I'm not complaining but...
Re:We need to talk about parentheses by PeterisP on Monday February 12, 2024 @09:38AM (#39345230)
Well, no, the above solution is to have a {} if and only if explicit scoping is desired instead of having just as many {} as there would be ().

Sometimes you need explicit scope for a bunch of things and the code would benefit from it. That doesn't justify having explicit scopes for everything all the time.

Re:We need to talk about parentheses by zelphirkalt on Monday February 12, 2024 @12:07PM (#39347363)
Isn't is always safer to have variables scoped only to the relevant part of the code, that is supposed to make use of them? So basically it would result in almost the same number of parens.
Re:We need to talk about parentheses by lex-lightning on Monday February 12, 2024 @12:34PM (#39347722)
This isn’t always possible, even in lisp. Compare to Rust’s non-lexical lifetimes
Re:We need to talk about parentheses by zelphirkalt on Monday February 12, 2024 @02:25PM (#39349123)
I don't see how it could ever be not possible. Maybe not always convenient, but impossible? How?
Re:We need to talk about parentheses by lex-lightning on Monday February 12, 2024 @02:32PM (#39349216)
a = getInt(); b = getInt() + a; c = getInt(); d = b / c; puts(d); puts(c); puts(b); puts(a);

Nothing in the middle of this code prevents using ‘a’ even though it isn’t relevant to ‘c’. And there’s no way to arrange it to change that.

Re:We need to talk about parentheses by zelphirkalt on Monday February 12, 2024 @03:37PM (#39350134)
If `c` does not depend on `a`, then the re-arrangement seems simple: Define `c` later.
Re:We need to talk about parentheses by susam on Monday February 12, 2024 @04:17PM (#39350640)
> If `c` does not depend on `a`, then the re-arrangement seems simple: Define `c` later.

How does that prevent 'a' from being in the scope where 'c' is defined? It is not clear to me what you mean by defining 'c' later. Later where? And how do we ensure that 'a' is not accessible there?

I think lex-lightning's comment has a point. We need to define 'a' and we need to define 'c' and then we need to print 'c' before printing 'a', so although 'c' does not depend on 'a', we still need to keep 'a' in scope because we cannot print it before we print 'c'.

Perhaps a convoluted solution to the problem posed in lex-lightning's comment would be something like this:

  #include <stdio.h>

  int getint() {
      return 42;
  }

  int main()
  {
      int a = getint();
      int b = getint() + a;
      int c;

      {
          // Shadow 'a' here to make the outer 'a' inaccessible in this scope.
          int *a = NULL;
          (void) a;
          c = getint();
      }

      int d = b / c;

      printf("%d\n", d);
      printf("%d\n", c);
      printf("%d\n", b);
      printf("%d\n", a);

      return 0;
  }
Output:

  $ cc -std=c99 -Wall -Wextra -pedantic foo.c && ./a.out
  2
  42
  84
  42
Re:We need to talk about parentheses by zelphirkalt on Monday February 12, 2024 @05:06PM (#39351194)
I see now how the problem is. Thanks. I have a suspicion, that this depends on side effects and that without assignments and without side effects things might look different. Somehow I don't seem to come across these scenarios, when I write FP programs.
Re:We need to talk about parentheses by lex-lightning on Monday February 12, 2024 @05:51PM (#39351654)
Hey I’m down to party with some monads, but I leave that as an exercise to the reader.

One note: each variable only gets assigned to once, so FP won’t magic that away.

The side effects might have a different way of management in FP. But the IO will happen in the same order in reality anyway.

So I don’t think there’s a way out, but I honestly would be interested to see it.

Re:We need to talk about parentheses by zelphirkalt on Monday February 12, 2024 @08:16PM (#39353077)
I will definitely keep an eye open now, to see whether I hit such a situation in the future.
Re:We need to talk about parentheses by lex-lightning on Monday February 12, 2024 @04:46PM (#39350975)
I appreciate your interest.

I’d argue that even declaring ‘c’ is against the spirit though. To say nothing of the eldritch shadow cast there (no disrespect, I think we’re both enjoying playing around here)

Re:We need to talk about parentheses by susam on Monday February 12, 2024 @04:58PM (#39351114)
> I’d argue that even declaring ‘c’ is against the spirit though.

Yes, I agree. My code example is meant to show the best we can possibly do to solve the problem in your comment and how contrived that solution is. The fact that this solution is contrived and diverges from the intended spirit of the problem only emphasizes the point of your comment.

That's why I am eager to understand what zelphirkalt really meant when they wrote, "the re-arrangement seems simple: define `c` later". It seems far from simple and, in fact, impossible if we want to avoid contrived solutions.

Re:We need to talk about parentheses by lex-lightning on Monday February 12, 2024 @07:25PM (#39352596)
I’m a contrived idea man, not a contrived solution man, damnit.
Re:We need to talk about parentheses by lmm on Monday February 12, 2024 @07:52PM (#39352870)
That's only a problem if your language doesn't allow separating computation from executing effects. With monads it's pretty straightforward:

    def doD(b: Int, c: Int) = puts(b / c)
    def doCD(b: Int) = getInt >>= {c => doD(b, c) >> puts(c)}
    def doBCD(a: Int) = getInt >>= {tb => val b = tb+a; doCD(b) >> puts(b)}
    getInt >>= {a => doBCD(a) >> puts(a)}
Re:We need to talk about parentheses by lex-lightning on Monday February 12, 2024 @08:12PM (#39353042)
If I wanted to deeply pedantic, I think the blocks associated with those monady bits keep the parameters in scope longer than needed.

But I never got into Haskell so I don’t really know.

Re:We need to talk about parentheses by lmm on Monday February 12, 2024 @11:58PM (#39354590)
> If I wanted to deeply pedantic, I think the blocks associated with those monady bits keep the parameters in scope longer than needed.

I don't think so? If you write it as a "flat" block using do notation then anything from earlier in the block is accessible later in the block. But local functions are real functions with lexical scope, they don't get hoisted up to the containing scope or anything. (And FWIW that's Scala-style syntax, although the ideas are very Haskell-like)

Re:We need to talk about parentheses by lex-lightning on Tuesday February 13, 2024 @07:13AM (#39356924)
Dope. Thanks.

Have you written much Prolog?

I was thinking about in Prolog terms and I think it’s the hardest paradigm to avoid the scoping problem in

Re:We need to talk about parentheses by lmm on Tuesday February 13, 2024 @05:23PM (#39363658)
I've never done any Prolog.
Re:We need to talk about parentheses by xmcqdpt2 on Monday February 12, 2024 @06:17PM (#39351914)
No it isn't. In C you often have to keep pointers around for longer than you need to so that you can free() them at the end of a block.

But still, the original post said that they have to deal with this:

> because I always see about 10 uninitialized variables at the start of each function, where half of those are used only once

That's bad!