News for hackers. Stuff that matters.
Re:Show HN: My Single-File Python Script I Used to Replace S... by knowsuchagency on Friday September 22, 2023 @10:44AM (#37612712)
It's a single script. Globals are fine--they're even marked as such.
Re:Show HN: My Single-File Python Script I Used to Replace S... by oblvious-earth on Saturday September 23, 2023 @01:23PM (#37625309)
> It's a single script

It's over 1200 lines of code, it's not like it's 100 lines of code and can fit on a single screen

> Globals are fine--they're even marked as such

I would argue that globals in this context are not fine from a code maintainability point of view.

By using globals here it's hard to know from a function call if it's going to mutate global state or not. If all the functions were methods of the same class instance, and other functions were just functions or part of some other class, then it gives you a clear grouping of calls which are related to mutating that state.

In general I would argue if you are ever in the situation of "I have more than two or three functions that are related to each other and they all need to mutate the same state so I use a mutable global" or "I pass around the mutable state via arguments" then make a class! It creates an obvious semantic grouping of callables.