Monday, 16 February 2015

Summary of Object Oriented Programming

Over the past couple of weeks, we have been working on object oriented programming, which is based on the concepts of "objects". Objects are data structures which contain data in the form of attributes and methods. On a fundamental level, the concept of object oriented programming does not seem so difficult. You build a program which uses objects that interact with one another. The objects are instances of classes. 
In the first few labs, we learnt to design classes, which was an extension of the material from CSC108. Then, we learnt about inheritance, ( a new topic for me), where a subclass is based on another, and we worked on desigining these subclasses.  I think that the worksheets and labs we did were straightforward, where we only focused on designing one or two particular classes. It was easy to get the hang of these problems and perfect the class design. 
The real tricky part came when it was time to do Assignment 1.  This was the first time that I noticed the sheer difference in the transition from 108 to 148. Instead of being spoon fed and told what to do for each and every function, we had to do the hard thinking ourselves. I read through the assignment multiple times, and was still having trouble deciding where to begin. Designing separate classes is easy, but when it comes time to put all this information together and make a program which is supposed to play a game, it takes a lot more intuitive thinking.  The information all made sense to me, but it is crucial to have a perfect understanding of all the classes and how they will be working together, before writing any code. This is one of the mistakes I made when I first started working on the assignment. I tried writing out code first. Then I realized I was making my generic Game State class specific for Subtract Square. I think for object oriented programming, you need to think on the broad scale, generic level, before working on any of the inherited classes. This works like a hierarchy.  I made a flowchart for myself, which I think helped me out the most, and I will be doing this for future assignments. I thought about the main program and how the classes would interact (ie. how the game state and the strategy would be chosen), then i thought about the generic classes, then about the subtract square subclasses. Once it comes time to write the actual code, it was not as difficult, as long as you have a proper layout. This way you avoid running into any confusion, because you could always look back to the flowchart. I think the reason that we were told we must implement __init__, __repr__ and __str__ methods for all of our classes is that these our the most important things to think about when designing the class. If you have a good understanding of how the classes is initiated, and how it would be represented, then you can think about all the other methods or attributes of that class. 
In conclusion, I was really happy with object oriented programming. After finishing up the game and playing subtract square a couple of times, I was happy to see how much I had learnt and how much progress I had made. 

Sunday, 8 February 2015

Tracing Recursion

Recursion has definitely been the biggest challenge so far in 148, and it definitely took me a while to wrap my brain around the concept. I started off having recursion explained to me by a friend in an upper year. As she started explaining, she started stumbling over her own words, and explaining the concept seemed to be confusing her (even though she is a master of recursion). You can appreciate how difficult it may be, if someone who understands it still has a hard time explaining it.
In simple terms, I would define a recursive function as one that calls upon itself within the body of the function. While this may not seem so difficult, it is of crucial importance to be able to learn how to trace a recursive function before you can actually start writing one. (I tried to do the latter first and my brain almost exploded).
The first couple of worksheets we got in class gave a good outline of the steps you would take to trace the function. We had to write out each step of the recursive call. After doing a couple of the same types of problems in our lab, it became easier to understand the concept of recursion. (As everyone says, practice makes perfect).  It is always necessary to break a big problem down into smaller steps. Once you master these elementary steps, you grasp a better understanding of the concept at hand. This is the way that I break it down for myself now:  The recursive function always has a base case, which is a condition that allows the function to stop recursion. If you know what the base case is, you know what the "end goal" of the function is, and what it should return for the simple problem. In terms of tracing, every call of the function is going to move closer to the base case. That means, every time the function is called it takes a new parameter which is pretty much a broken down version of the first parameter. What appeals to me is that even though it is a difficult concept, the code for a recursive function is very clean and concise (imagine having to rewrite the code over and over instead of just using recursion).
Even though I have tracing down, I am eager to see how I will do in terms of actually writing recursive functions.

Sunday, 25 January 2015

Week 3: Why geeks should write

    On my middle school report cards, I remember being graded on writing under the 'Langauge' category, which was a different category than both my math and science courses. With this notion, I learnt to distinguish between science and writing as two completely separate entities. In essence, being good at math and science didn't have to mean I was a good writer and vice versa. My marks were always higher in the former. For this reason, I viewed writing simply as a nuisance and something I wasn't good at.
    I am currently in my fourth year of study, in a primarily life sciences background. Throughout my past four university years, most of the evaluations I have had were in the form of multiple choice questions. Writing wasn't stressed to be of much importance as my pencil was only required to fill in a bubble, allowing me to receive a high mark. But what most people don't know is this ugly truth: writing is crucial in every field and for success in everyday life. In my eyes, communicating information through the written word is just as important as verbalizing your opinions and being a good speaker. There is no use in knowledge if you are not able to write or speak about it and communicate with others. Therefore, middle schools should consider putting a writing category under every subject, not just English. Universities should also take the initiative to introduce more writing into a science curriculum (through the use of blog entries in a computer science class for example...).
    It is silly to even fathom the idea that programmers need not know how to write. In reality, programming itself is just writing, only instead of the english language, we are using a formal constructed language such as Python or Java to communicate instructions to computers. There is also a reason we put so much stress on being able to write a good docstring and good comments, in order to  clearly communicate to other programmers the function of our code so that they are able to understand it and use it. Being a good writer is going to separate a good programmer from a sloppy one.
    Writing is not only important to communicate ideas to others, but also to keep track of our own. As the popular saying goes, "Fool me once shame on you, fool me twice, shame on me". Being fooled by a bug or a programming error is much less likely to happen a second time around if we write our mistakes down and use them as a tool to keep track of information. By tracking these mistakes and writing out steps to a solution, we are more efficient at solving our own problems, and may save us valuable time in the long run.