Monday, 16 February 2015

CSC148 OOP Post #4

Reading week is here! Which means we are already half way through CSC148, I guess time flies by when you're busy programming! CSC148 has been great so far and I hope that it will be even better after the break. Throughout the first half of the course, we mainly focused on Object Oriented Programming or OOP for short. So in this slog post, I'm going to spend some time talking about OOP, why we need it, and where we use it.

In my second slog post, I did talk a little about OOP and the concepts that we looked at so far including creating classes and objects, special methods, and inheritance. In general, in OOP, the idea is to create objects of a particular class. When we instantiate an object, the object can come with properties and methods that belong to that object.

In Python, this is what we call the __init__ method, which assigns the properties to that object. We then have special methods such as __str__ which when called gives a string representation of the object, __eq__ which compares two objects together, and __repr__ which gives a string that can be put into the shell to create to create another object with the same properties. Objects also have normal methods where they just basically normal function calls.

Now inheritance is the concept of extending a class. In my second blog post I said that inheritance is not 100% needed to make your program work, but it helps reduce code, which is what the purpose of inheritance is. There are times where in a parent class, code may or may not be useful in some cases, but it may be very useful in other classes. So instead of rewriting the functions into a different class, we can extend the parent class onto the sub-class. The sub-class will "inherit" all the functions and properties of the parent class, thus reducing the amount of code in your program.

Okay, so why is this all important? Why do we need OOP? Well like inheritance, we want to eliminate any duplicate code, and sometimes, code can be changed. Professor Heap told us that code is always being fixed or added to create new features. However, if we drastically change our code on our end, it could spell problems on the client end. Essentially OOP is needed to easily change code.

If you copy and paste code all over the program, then when something needs to be fixed, all the duplicate code has to be fixed as well. If you accidentally miss something, let's just say good luck finding the bug. But with functions, you can change it once, and call it throughout the program. This is why we write methods and classes, which eliminates the need to duplicate code. And by reducing the amount of code in our function, we can also have a faster run time, but I'm going to leave that topic for another day.

Speaking of duplicate code, recursion does a great job in handling these sort of things. Since the idea of recursion is for a function to call itself, it eliminates the need to manually write out code for each case. I guess in a sense, recursion is sort of like a loop.

I came across another slog where they talked about how to come up with the design for classes from the description that they give you and they also talked about inheritance with a good easy example. You can check out his slog here.

So that is my summary of OOP, why we need it, and where its used in our programs. Thanks for reading!

No comments:

Post a Comment