Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

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
share|improve this answer
    
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. –  L. De Leo Aug 28 '12 at 22:49
    
+1, great youtube link –  John McDonald Aug 29 '12 at 1:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.