News for hackers. Stuff that matters.
Re:Solid Relevance by nendroid on Monday October 19, 2020 @08:20PM (#24832594)
>Yeah, I suspected that you and lerptime were the same person.

to get around the post limit. Keep it on the DL.

>When you make such a statement, you sound like someone with fairly limited experience. In particular, from your example, I suspect that you've never worked in a multi-threaded embedded system. somethingToBeInSyncTo, done that way, means that any other thread holding a reference, holds a reference to the old value, while this thread has the new one. And updating running threads with new values on the fly is... problematic.

I find that you're the one who seems inexperienced. My example is thread safe ... makeNewState is a combinator. It makes no mutations. Like IO, and mutations, the problems with threading happen with mutation. Thus, locks and other procedural commands used to deal with threading live in the void IO function.

We're more referring to coupling functions with external data and mutations via objects. If you're talking about synchronizing two threads, this again needs to be handled with parent IO function outside the logic of your pipeline combinators. You can still segregate logic away from threading and IO.

My combinator in this case is useable for your example case of synchronizing two threads. If you organized your code in ways where my combinator will be unusable than you have failed to decouple your logic away from threading, IO and mutation.

Re:Solid Relevance by AnimalMuppet on Tuesday October 20, 2020 @12:25PM (#24839262)
> I find that you're the one who seems inexperienced.

35 years professional software engineering, 30 years in embedded. I'm going to guess that you have less.

Your way of organizing code works great, on paper. Maybe on a small project. For a real embedded system, with multiple threads, with state data being used everywhere... your approach doesn't make much sense. It makes a much worse design than shared mutable state. (Yes, shared mutable state is in fact as evil as you have think it is. It's still better than trying to make your alternative work in that environment.)

Why is state data used everywhere? Because embedded systems often respond in different ways depending on the state of external inputs, and don't go read the state at the time they make the decision on how to respond. If those decisions are spread through the bulk of the code, there's really not much left to put in your combinator. And there's no point in trying to add that paradigm to a system that's going to have tons of shared mutable state anyway.

Re:Solid Relevance by nendroid on Tuesday October 20, 2020 @12:57PM (#24839706)
>35 years professional software engineering, 30 years in embedded. I'm going to guess that you have less.

Doesn't change the fact that you're wrong and 35 years of programming doesn't ensure that you're a good developer. There are examples of crap code and patterns that have been around perpetually.

>Why is state data used everywhere? Because embedded systems often respond in different ways depending on the state of external inputs

State being everywhere doesn't preclude it to being segregated from logic.

>If those decisions are spread through the bulk of the code, there's really not much left to put in your combinator. And there's no point in trying to add that paradigm to a system that's going to have tons of shared mutable state anyway.

The decision is what goes in the combinator.

   #IO functions.

    Data getIOFromModuleA()
    void sendIOToModuleA(Response r)

    Data getIOFromModuleB()
    void sendIOToModuleB(Response r)
       
    ....


   # combinators, reuseable in both module A and module B because of segregation from IO and State

    Decision makeDecision(Data x)
    Response createResponse(Decision x)
It's the same pattern for State. Just replace the word IO with State. Response can be anything, it can be the entire new state of the program. It can represent a delta of a change, it can be a response specific to a module. Also again, all threading commands go into State or IO functions.

OOP fails to segregate code this way and if you don't organize your code like this then you have 35 years of experience of navigating someone elses mess or your own.

Re:Solid Relevance by AnimalMuppet on Tuesday October 20, 2020 @01:18PM (#24839964)
So I said you were inexperienced. You said I was. I listed my experience. You said it was the wrong experience, but conspicuously declined to state your own.

So I'll just leave you with this. "In theory, there is no difference between theory and practice. In practice, there is." You've got your theory. Nice theory. Use it for a decade of actual real-world work, and let me know how it works out. I'll be especially interested if any of that work is in embedded.

Re:Solid Relevance by nendroid on Tuesday October 20, 2020 @02:31PM (#24840796)
I said it as a minor minor retort. You want the full story? The full story is I don't care for experience. I find experience doesn't correlate with actual skill. Many experienced developers act and seem very inexperienced which is what I said about you: You "seem inexperienced" because you act that way. The very act of bringing up experience is off topic and a sign of lack of maturity.

If you have to know I'd say I have about 15 years of experience in research, gaming, embedded and web. I will say that the pattern I described can be followed in every one of those fields but it's a niche pattern because it is indeed promoted by people who do FP.

I don't know why you focus on embedded, but embedded people tend to follow these patterns the least due mostly to C++. The overall promoted pattern in C++ is to pass references everywhere and it's highly unlikely that someone in that world will follow this pattern because you have to go out of the way to do it and learn about it. It requires a disciplined approach.

The big problem is if you need to slightly modify a bunch of values in an array of 100 objects. It's hard to figure out the right way to do it in a combinator. The common pattern used nowadays is instead of having combinators handle it, have the combinators generate logical instructions to send to the IO function similar to how in web development your stateless servers pass SQL instructions to the SQL server. The IO function processes the instructions and handles the threading, mutation and IO. Here's a library that does this:

   https://github.com/google/cpp-frp <---- real world example.
Nothing I said is "theory" in the sense that you put it. These are rare but actual patterns that are used successfully by people in the know. You are not in the "know" despite your experience. As a result for most of your program experience you've just been dealing with and fixing mess after mess after mess.
Re:Solid Relevance by AnimalMuppet on Tuesday October 20, 2020 @05:17PM (#24842427)
"It is useless to have a conversation with those who will not listen." Neither of us are listening, because we're both sure we're right. So I'm out.

You get the last word, if you want.

Re:Solid Relevance by nendroid on Tuesday October 20, 2020 @05:29PM (#24842529)
Don't be stupid. I'm listening to you. I'm actually responding to you. You say theory is different from the real world so I present you with a real world solution and examples of how it works with embedded specifically. Your job should you choose to continue is to use other real world examples to counter my arguments. That's all, if you convince me it's done, If i convince you it's also done. Until then leaving the discussion before it's resolved is pointless.

Don't back out, I'm not fighting for the last word. I'm having a discussion with you in hopes you can convince me. But you haven't really said anything substantial other than things along the lines of "Trust me I have lots of experience, your stuff is just theory."

If you start introducing the real arguments then this discussion can actually go somewhere.

Re:Solid Relevance by AnimalMuppet on Tuesday October 20, 2020 @06:39PM (#24843033)
Fine.

  void handleEvent1() {
    if (currentState == state1) {
      currentState = state2;
      setHardwareState2();
    } else {
      currentState = state3();
      setHardwareState3();
    }
  }
You can move state to the boundary, and IO to the boundary, and then... what's left? It was all boundary.

A bit more realistically, here's an automobile cruise control that uses the same button for "reduce set speed" and "set the speed". (Don't blame me, I've had more than one car with that exact setup).

  void buttonXEvent() {
    if (cruiseControlEnabled) {
      if (cruiseSpeedSet) {
        cruiseSpeed--;
      } else {
         cruiseSpeed = currentSpeed;
         cruiseSpeedSet = true;
      }
      writeSpeedToEngineComputer(currentCruiseSpeed);
    }  // else do nothing
  }
How would you write this in your approach?
Re:Solid Relevance by nendroid on Tuesday October 20, 2020 @07:47PM (#24843475)

  //combinators
  CruiseSpeed = int

  CruiseSpeed getNextCruiseSpeed(bool cruiseControl, bool cruiseSpeedSet, int cruiseSpeed, int currentSpeed) {
      if cruiseControl && cruiseSpeedSet
         return cruiseSpeed - 1
      elif cruisControl && !cruiseSpeedSet
         return currentSpeed
      else
         return cruiseSpeed
  }

  bool getNextCruiseSpeedSet(bool cruiseControl, bool cruiseSpeedSet){
      return cruiseControl && !cruiseSpeedSet ? true : cruiseSpeedSet
  }

  //IO functions
  void getCurrentSpeed()
  bool isCruiseSpeedSet()
  void setCruisSpeedSet(bool cruiseSpeedSet)
  void writeSpeedToEngineComputer(CruiseSpeed x)
  void handleButtonPress(bool cruiseControl, bool cruiseSpeedSet, int cruiseSpeed, int currentSpeed){
     speed = getNextCruiseSpeed(cruiseControl, cruiseSpeedSet, cruiseSpeed, currentSpeed)
     cruiseSpeedSet = getNextCruiseSpeedSet(cruiseControl, cruiseSpeedSet)
     writeSpeedToEngineComputer(speed)
     setCruiseSpeedSet(cruiseSpeedSet)
  }
Your example uses free variables extensively. It means that all your logic cannot move outside of the context of the free variable which means they cannot be reused. Even IO functions should not use external context.

Combinators can be reused everywhere.

Re:Solid Relevance by AnimalMuppet on Tuesday October 20, 2020 @08:12PM (#24843646)
I see no respect in which your solution is superior.

By the time you implement getCurrentSpeed(), etc., you're going to have a fair amount more code than me. (writeSpeedToEngineComputer() doesn't count, because I need to implement that also.)

Your reason for doing so is, I think, in your last two paragraphs. But this isn't particularly reusable code - I'm not going to use getNextCruiseSpeed() or handleButtonPress() in the windshield wiper controller. It's pretty much tied to the specific use. (There may be a handleButtonPress() in the windshield wiper controller, but I can't reuse this one.)

So you offer me something that is more verbose, based on a benefit that I can't actually benefit from.

(I presume that getCurrentSpeed() returning void was just because of quick typing.)

Re:Solid Relevance by nendroid on Tuesday October 20, 2020 @09:39PM (#24844074)
The point was to prove that I can pull the combinator out of your unsegregated code and moularize IO away from logic.

The other point of this was to improve code modularity in every corner of your code. Why is this good? If all your logic can be shifted around then that means that you can reuse code to anticipate future changes.

The most insidious form of technical debt is unanticipated technical debt. You encounter it when a new requirement makes you realize that you can't reuse segments of your code. You realize that you grouped things together incorrectly. You failed to have the foresight to make your code more modular.

It's a problem people think they can solve in hindsight but they can't in reality because they cannot predict the future so they will inevitably group code incorrectly. And therein lies the problem: grouping. Don't group your code. Make all your logic modular and just organize your code with superficial schemes like namespaces.

The problem with OOP is that the very nature of an object is a grouping. OOP is all about grouping the code around shared mutable variables within a context. You are forced to assume a grouping and you may be right in some cases (like cruisecontrol) but you will inevitably be mistaken in many other cases. This "mistake" is the origin of the most insidious form of technical debt.

The other problem with OOP is that they think that this style of programming is modular. They have no idea how un-modular it is. So they use it extensively with patterns thinking they're improving modularity.

The most modular primitive is the combinator. That is why to prevent technical debt as much as possible, the key is to move as much code as possible into the combinators.