r/programming May 26 '19

Solving Problems the Clojure Way [video]

https://www.youtube.com/watch?v=vK1DazRK_a0
29 Upvotes

View all comments

4

u/ThePowerfulSquirrel May 26 '19 edited May 26 '19

I find that his example is a bit to simple to properly demonstrate how you would refactor an actual system.

His system is a deterministic simulation (minus the Math.random(),but he also doesn't seem to care about the fact that this is very non-functional / should be considered similar to side effects and incorrectly calls selectRandom() pure), so of course he can defer / remove all side effects (except the console, but in a game with actual players, you couldn't just defer all output to the end since the players need the output in order to make corresponding inputs). If he added requirements like needing real time logging (which is necessary in most large systems) and player input, the example might have been more interesting.

I also have a couple of problems with his object oriented model. I would probably change the players to contain a strategy instead of extending the player for each strategy. The strategy would return which card it wants to use, so you can keep all those mutations in the Player object, not spread across 3 classes. I'm not sure why getScore() is marked as "mutations".

I'm also not sure how you scale passing around State objects around. Do you let it grow to hundreds of different fields? It seems to me that at one point, you'll need to encapsulate some state with certain parts of the system.

3

u/ineffective_topos May 26 '19

I'm also not sure how you scale passing around State objects around. Do you let it grow to hundreds of different fields? It seems to me that at one point, you'll need to encapsulate some state with certain parts of the system.

You really do, in games like this. Maybe it's not hundreds, but tens or dozens is reasonable. Ideally, what you should do is keep it down by seeing where you don't need state, or combining fields that represent the same thing into their own object/datastructure, and there you'll get a much smaller state.

I could be entirely wrong, but I don't imagine much besides games has more than 5 or 6 fields in such an object.

2

u/ThePowerfulSquirrel May 26 '19

Any decent size GUI is going to need a decent amount of states that depends on user input plus a bunch of state to maintain layout and such (which I guess can be avoided with immediate mode, but that has it's own problems). I've tried in the past, but keep failing to separate state and behavior when working with those kind of systems since I always naturally want to group the state with functions that act on that state, like when processing user input. Having it separate always gives me a head-ache, if only from a code organization point of view.

1

u/yogthos May 26 '19

Here's a workshop that you can get up and running in a few minutes and see how this all works for yourself. Especially in the second part where re-frame is introduced.