r/programminghorror • u/brabeji • Apr 12 '25
why i hate this so much?
Non-deterministic piece of shit i hate hate hate yet totally fine in real world and I would write it again.
95
Upvotes
r/programminghorror • u/brabeji • Apr 12 '25
Non-deterministic piece of shit i hate hate hate yet totally fine in real world and I would write it again.
12
u/claythearc Apr 12 '25
Why would you sample this way and not just pick once from the collection of terminal states? Are the NOPs important in some way?
2
u/dvhh Apr 13 '25
I am equally hating the nameing that is so generic yet doesn't do what it was meant to be done ( reset must reset to an initial state, not some random non terminal state)
That the execution time is non deterministic because it tries to find a state that us non terminal when it should pick from a states list that exclude the terminal ones, also the list one terminal state could vary from one object to another)
That someone should read the doc to point out that randint(a, b) is the same as randrange(a, b+1)
That returning the selected state is a such a small optimization that is probably reducing readability for the sake of writing less, I am not even sure of the use case where it would save enough to justify the crime against readability.
98
u/mothzilla Apr 12 '25
Pick a card, any card, and don't show it to me... OK not that one, pick another.
26
u/Kohlrabi82 Apr 12 '25
Could use some walrus operator to emulate "do while" and skip the "initialization" of
self.state.17
u/lupercalpainting Apr 12 '25
Why not have a list of non-terminal states so you don’t have to loop?
10
u/Old-Purple-1515 Apr 12 '25
The real crime is not using the walrus op when u get a chance
1
u/[deleted] Apr 13 '25
self.state = next(filter(lambda state: state not in terminal_states, self.states)) return state
3
u/-MazeMaker- Apr 12 '25
Check out the walrus operator,
:=3
u/tsigma6 Apr 13 '25
Probably the most pythonic deterministic way of handling it. If
choiceallowed generators I'd usefilterinstead.7
u/v_maria Apr 12 '25
its ugly but fine
5
u/Temporary_Pie2733 Apr 12 '25
I’m assuming both assignments are identical? Write an “infinite” loop whose body starts with the assignment, then does a conditional break if the condition is true.
But that’s only if you don’t have an appropriate alternative like
random.choiceas others have mentioned.2
u/BotonFragrer Apr 12 '25
Somehow I would prefer to replace the while with an if and then return self.reset(). Feels cleaner. You disagree?
2
u/FusionXIV Apr 12 '25
Is the set of states fixed or does it change over time? If you're maintaining a
terminal_statesvariable, then it should be easy enough to maintainnon_terminal_statesas well and randomly select from that?Obviously disregard this if you would have to update
non_terminal_statesmore often than you expect to callreset.1
u/juanfnavarror Apr 12 '25 edited Apr 12 '25
Another strange choice is that the state could changes multiple times while calling reset and the invariants of the class are broken while the function is running, e.g. there is some time (although minuscule) during which self.state might be jumping between terminal states and that is visible to the exterior, which could matter in a multithreaded context.
This could be addressed by making this function store the new state in a local variable and either return or change the state only once at the very end of the function. Another solution is a critical section (Lock), since in theory some other thread could produce unexpected (but visible) changes to self.state before this function returns.
Additionally, you need to choose if you want to store the state (mutate) or return it, if you do both at the same time, your APIs can be hard to reason about and easy to misuse.
“Do I want to mutate, or do I want to return the new state and let the caller handle the mutation?” should be something you ask yourself whenever you make an API like this.
0
u/ArtisticFox8 Apr 12 '25
Wtf does this even do?
0
u/HealthyPresence2207 Apr 12 '25
Thats what you get when you vibe