I have been working on a simple implementation of a card game in object oriented Python/HTML/Javascript and building on the top of Django.

At this point the game is in its final stage of development but, while spotting a big issue about how I was keeping the application state (basically using a global variable), I reached the point that I'm stuck. The thing is that ignoring the design flaw, in a single-threaded environment such as under the Django development server, the game works perfectly.

While I tried to design classes cleanly and keep methods short I now have in front of me an issue that has been keeping me busy for the last 2 days and that countless print statements and visual debugging hasn't helped me spot. The reason I think has to do with some side-effects of functions and to solve it I've been wondering if maybe refactoring the code entirely with static classes that keep no state and just passing the state around might be a good option to keep side-effects under control. Or maybe trying to program it in a functional programming style (although I'm not sure Python allows for a purely functional style).

I feel that now there's already too many layers that the software (which I plan to make incredibly more complex by adding non trivial features) has already become unmanageable.

How would you suggest I re-take control of my code-base that (despite being still only at < 1000 LOC) seems to have taken a life of its own?


1 Answer 1


What you experience right now is a typical consequence of keeping a complex global state. Global states, and complex global states in particular, make side effects hard to track and your code impossible to test. Which is why, whenever something goes wrong, you have to debug your system the hard way.

Your program has "only" 1.000 LOC, so not everything is lost. Just do the following:

  • Watch http://www.youtube.com/watch?v=-FRm3VPhseI
  • Take pen and paper and think about how you can redesign your system without global states
  • Implement unit tests and make sure that your classes pass these tests
  • Reassemble your classes according to your new design
  • \$\begingroup\$ Thank you BerndBrot, sounds like a good plan. Right now I'm keeping state in a Redis server where for every Game instance I have 1 key and as value 1 pickled Game object that contains everything. I guess that still counts as the global state anti-pattern you mention. \$\endgroup\$
    – Lorenzo
    Commented Aug 28, 2012 at 22:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .