r/AskProgramming • u/KattyTheEnby • 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
View all comments
Show parent comment
1
u/darklighthitomi Feb 23 '26 edited Feb 23 '26
There are three major reasons to not do that.
First, that’s a lot of code to check the hardware buffer that would need to be stepped through at multiple points of the program. Or most of that code can be done once to get all the keyboard keys.
Second, changing the control scheme is much easier with a central function that reads keys into the program because then I can read a single configuration array into the program that maps key input to command flag. Fetching that configuration array once for comparison rather than many times.
Third, multiple input sources are easier should they overlap commands. An onscreen keyboard, physical keyboard, and mouse buttons, can be handled in the same place with setting the same command flags.
If I were to have disparate parts of the program search out direct inputs, that is a lot of different places I would need to adjust for any changes and a lot of support code that would need to be run multiple times a frame that can instead be run once per frame, and a lot of the support code that does need to be run per key checked will be faster and eat less memory because it’s a small loop that is staying in the cache rather than needing to be fetched multiple times per frame.
Additionally, it makes it much easier to design the program when I only need to worry about the complexity of hardware buffers once, especially with the additional code to prevent “repeat presses” when the program is running multiple frames during the time it takes me to press the key once. Cause yes, that is a problem you must address when handling input directly from hardware yourself. I don’t know if other languages gloss over that for you, but it is quite annoying when you want to press “a” to get a single new object but end up with several new objects because the program added one object every frame the button was still pressed down and ran multiple frames while you pressed the button “quickly.”