Wednesday, 25 February 2015

Week 6: Review of OOP

Last summer I was playing a lot of Hearthstone, an online card game similar to Magic: The Gathering. I wanted to know the probabilities associated with drawing certain combinations of cards before a given turn. I tried to create a program that simulated a very large number of games while keeping track of which turn particular cards were seen by. This was before I was familiar with object-oriented programming so I approached the design of this program by constructing functions that attempted to create, mutate and examine lists of tuples. I was very disappointed with how the program took shape. Even with helper functions, the code was very hard to read, the overall design felt needlessly complex and eventually I gave up on it.

About a month ago I spent some time trying to redesign this program using OOP. Although I have yet to complete this program, it's clear that an object-oriented approach has significantly improved its design. Having attributes stored in classes helped me come up with a much more precise plan for what data I was tracking and what methods I need to make it all work. Figuring out how Card, Deck and Game objects should interact feels easier (and less error-prone) than figuring out how generic lists and tuples should interact. This resulted in the new implementation being significantly leaner than the old one. Also, while the original design had methods and data arranged without any apparent organization, having methods and data stored within classes has made the code more organized and readable. I found that OOP is very conducive to having organized code. This had two important consequences. First, it was much easier fix the code when a bug was found. Second, it is now much easier to extend the functionality of the program. I already have a few ideas for new questions that this program can answer and I anticipate that these changes will be significantly more straightforward to implement relative to the old design.

Although I'm sure that my program could be made to work without OOP, this experience showed me how in many cases, using classes is just the most natural and effective way to approach program design. Classes structure your thinking and they play a major role in organizing your code.

5 comments:

  1. Great post! I agree 100%, even though you don't have to use Object Oriented Programming it makes the code a lot easier to read as well as use. It also makes it easier to use the code/class in other algorithms. I really liked how you used an example to show the benefit of OOP, rather than just stating it is better.

    ReplyDelete
    Replies
    1. Thank you for the feedback. I'm glad you liked the post!

      Delete
  2. I've actually never thought about designing something of my own. I don't know which game you are talking about, but it's interesting that you are taking initiatives by practicing on something you enjoy. Although, I might note that while Python is certainly an object-oriented programming language, there are other programming languages that use different ways to interpret store data.

    I guess when we learn those other programming languages, we'll both learn how other programming languages work that use alternatives to OOP.

    ReplyDelete
    Replies
    1. Recently I've been reading some very basic introductions to C, which is not an object-oriented language. I'll definitely make use of these lessons I learned about code organization when the time comes to write complex code in languages without classes.

      Delete
  3. I've actually never thought about designing something of my own. I don't know which game you are talking about, but it's interesting that you are taking initiatives by practicing on something you enjoy. Although, I might note that while Python is certainly an object-oriented programming language, there are other programming languages that use different ways to interpret store data.

    I guess when we learn those other programming languages, we'll both learn how other programming languages work that use alternatives to OOP.

    ReplyDelete