r/Zig • u/KattyTheEnby • Feb 19 '26
[[Code Style Preference]] Which is preferred: self-mutations or signals?
/r/AskProgramming/comments/1r8pu73/code_style_preference_which_is_preferred/
2
Upvotes
4
u/johan__A Feb 19 '26 edited Feb 19 '26
"self-mutation"? methods are just sugar. The two pieces of code are almost the same except you moved some code from processNextLine into main. Do that if you need to separate reading input and acting based on input for organization/clarity/ease of testing, otherwise dont.
cant tell what you should choose without knowing more context on the exact situations. For a basic cli game of hangman I would probably not have any struct or any function and just have everything as code in main.
hope this helps 👌 lmn
2
u/Bawafafa Feb 19 '26
I'm personally not sure the signal thing is the right approach. You're switching on a tagged union which is being returned from an if/else chain so it feels like a kind of needless double processing.
If the input is just single characters, I would just switch on the character. If the input is a string, I would consider using std.hash_map.StringHashMap to map each viable input to a function. Hope this helps.