Saturday, February 14, 2015

SLOG Post #4 - Summary of Object-Oriented Programming

This week I will be giving my thoughts on Object-Oriented Programming (OOP). OOP has been the focus of csc148 up until this point, and one of the major factors that distinguishes it from its prerequisite, csc108. While it is true that we learned how to define classes and class methods, we never really learned what they were used for. In fact, I remember thinking that they were pretty useless, just a laborious way of storing information that could be stored in other ways (like a dictionary). However, I know now that this is far from the truth: OOP is used almost everywhere, and is one of the most important developments in programming as a whole. Many of the most common languages, like Java, C++, C#, Perl, and of course Python, support a an object-oriented paradigm. It is a necessary tool for any programmer to have.

What OOP is:

OOP is a form of programming that centres on objects, data structures that contain attributes (the data that is being stored) and methods (functions that can operate on the object and access/change the attributes). In our class, we used Python to define new classes of object that represent different kinds of information, like the location of a point on a Cartesian plane or a shopping list with various items. Python already some with a bunch of pre-made classes (like str, int, list, etc), but making your own classes can be much easier than working with the ones that you're given in certain situations.

What inheritance is:

One of the biggest (and in my opinion, coolest) concepts in OOP is class inheritance. This is the idea that you can define a general class that contains a group of more specific (but related) subclasses. The general class never even needs to be used! It's just there to store attributes and methods that are common to the subclasses. For example, you could have a general class "Animal" and multiple specific subclasses like "Dog", "Cat", and "Zebra". In your general class, you'd have methods that work for all animals (like find_mate() or eat()), and in the specific classes you'd have the more specific methods (like fetch() for class Dog or purr() for class Cat). You can also put methods in you general class that are common to all subclasses but implemented differently in each one: all you need to do is define the function and raise a NotImplementedError exception. We put this property of OOP to good use in Assignment 1 (see SLOG post 3).

Challenges:

Overall I didn't find OOP (and inheritance) too diffuclt to grasp. Indeed, the hardest part was getting used to the formatting conventions for designing classes, in particular the docstrings for the different methods. However, I do occasionally find it difficult to understand exactly what kinds of methods a class would need given a written description. Luckily, in the computer labs we had many chances to practice doing this sort of excercise, and I think that I got much better at doing it (my term test mark was much higher than my quiz marks for labs 1 and 2).

Other SLOGs:

I really enjoyed reading the post about OOP on myslogisbest.blogspot.ca (post on Feb. 20). I found the slogger's views to be generally in line with my impressions of both the course and the object-oriented programming style. I also liked the diagram of object-oriented programming on jerrycsc148slog.blogspot.ca (post on Feb. 14), it's cute and it definitely illustrates the idea well.

Saturday, February 7, 2015

SLOG Post #3 - Term Test

Note: According to the course website, this blog post was supposed to discuss tracing recursive functions, but I did that last week so I decided to talk about the test instead.

This week we had our first CSC148 midterm test. I was pretty worried about it because I wasn't sure what kind of questions would be on it, as all of the past tests that we were given had been written later in the semester than we were writing ours, and they focused on topics that we hadn't covered yet. I studied by going through all of the lectures and course notes, and by re-doing the labs. I also did some of the extra practice questions that were assigned with the labs. I found these questions to be quite difficult, especially the Reverse Polish Notation question from the Lab 1 and Lab 2 problem set. Because the questions didn't come with answers, it was difficult to determine if I was answering the questions correctly. I nonetheless think they helped me a lot.

I was pleased with how the test went and I think that I was very well prepared. There were three major questions: one on creating a class, one on tracing recursion, and one on implementing subclasses for an abstract class. I had been worried that there would be a question where we would need to create a class from scratch and implement all of its methods, but luckily there wasn't. The test was quite long for a 50-minute writing period, and I finished just on time. It was one of those tests where if you spent too long thinking about a question, or if you did something wrong and had to go back and re-do it, you would likely not finish on time. That is why I was really glad that I had studied well: for each question, I knew almost immediately what I needed to do. The "cheat sheet" was also a big help: even though I already knew most of what I wrote on the sheet, it was really helpful to see it right in front of you.

I think my studying method was pretty effective (if anything, I feel that I over-studied)!  I would definitely use a similar approach for our next test.