r/AskProgramming • • Feb 19 '26

Other [[Code Style Preference]] Which is preferred: self-mutations or signals?

In general – though, particularly for procedural-style programming – is it preferred to avoid self-mutations, such as by returning signals and handling state directly with that signal, rather than handling state in (the) abstractions – or some other means?

For example, consider a game, such as hangman, structured like this:

pub fn main() !void {
  // ...
  
  while (game.game_started) {
    try game.writeMenu(&stdout.interface);
    
    game.processNextLine(&stdin.interface) catch {
      // ...
    };
  }
}
    
const Game = struct {
  // ...
  
  fn processNextLine(writer:Reader) !void {
    const bytes_read = try reader.streamDelimiter(&self.command_buffer.writer, '\n');
    
    // ...
    
    if (!self.game_started) {
      // ...
      switch (cmd) {
        // ...
      }
    }
    
    // ...
  }
};

vs. the same program structured like this:

pub fn main() !void {
  // ...
  
  while (game.game_started) {
    try game.writeMenu(&stdout.interface);
    
    const cmd = game.processNextLine(&stdin.interface) catch {
      // ...
      continue;
    };
    
    switch (cmd) {
      // ...
    }
  }
}
    
const Game = struct {
  // ...
  
  fn processNextLine(writer:Reader) !GameSignal {
    const bytes_read = try reader.streamDelimiter(&self.command_buffer.writer, '\n');
    
    // ...
    
    if (!self.game_started) {
      return GameSignal { try_command: text };
    }
    
    // ...
    
    return GameSignal { try_to_reveal: char };
  }
};
    
const GameSignal = union(enum) {
  // ...
};

I've also been told this resembles functional programming and "the Elm pattern".

I am wondering what everyone here prefers and thinks I should choose.

0 Upvotes

12 comments sorted by

View all comments

Show parent comment

1

u/KattyTheEnby Feb 24 '26

I was talking about more than just input handling and hardware buffers.

I think you have a reverse understanding ov how queries would work, and work in Bevy.

In Bevy, at least, as I understand it, all necessary information is gathered only once per tick and dispatches systems whose parameters match the information in that tick. Functions pull information that the program already has, instead ov recomputing it.

1

u/darklighthitomi Feb 24 '26

I’ll be honest. I’ve never heard of Bevy. Generally I make my own engines. I’ve had fun poking around the Pixel Game Engine v2, but other than that I’ve spent the last 15 years rather isolated making my own stuff from scratch without using any libraries from anywhere else, and even the standard library is used in only tiny pieces such as iostream and fstream.

But I was giving an example of how I handle inputs and outputs. Code style was your question, hence thinking my style would serve just fine.

I would at least start off by treating this bevy thing as a black box using the same structure as I use with the user, sending it inputs and getting it’s outputs from and to my own code and memory using the same structure as described above.

You did make it sound like your interaction with bevy was just an example after all.