Sunday 13 October 2013

OOP and recursion

The assigned topics for this week that I'll be talking about are Object-Oriented Programming, and recursion. These two words pop up pretty often in the programming world but what exactly are they and why do we care?

Object-Oriented Programming, or OOP for short, is a programming model where everything can be represented as an object or a class. What is an object/class you say? Well in truth it can really be whatever you want it to be.

For example, in Python a string is an object. In this case, it's a sequence of characters which have certain characteristics or "attributes", like their length and also certain functions or "methods" that can be applied to it, like the join() method. At first the purpose of OOP may not be obvious, but the main benefit is that it allows us to abstract physical things and concepts that we have in the real world, and create a recipe for it in a program. Using the previous example, a string is really just a recipe for representing some text. An instance of a class, for example of a string, is a specific example of that class. Something like 'hello' would be an example of a piece of text, or using OOP lingo, an instance of a string.

Another benefit of OOP is the concept of hierarchy, which is a very human and natural way to classify things. For example, let's say there is a zookeeper who wants to represent his zoo in the virtual world. He would like to be able to classify the animals in his zoo, but also store attributes (what's their name, what do they eat, how many legs do they have) and methods (feed the animal, clean up their enclosure) related to each animal. A natural way to approach this would be to create a class (a specific object) called Mammals, which could have an "number of legs" attribute of 4. Then, he could create a subclass of mammals called lemurs, which would have a "food" attribute of bamboo. Finally if he had a lemur in his zoo called George, he would create an instance of a lemur with the name "George". In OOP, a subclass inherits from its superclass, which is what allows for this kind of organization.

In short, OOP allows programmers to abstract ideas from the real world and represent them in a program, whether it be plain text, animals, or stacks.

Another concept which allows programmers to bring real world thinking into a program, is the concept of recursion. Recursion is essentially the idea of breaking a task down into smaller bits, until the pieces of the task are small enough to be solved very easily, and then finding the solution by rebuilding all the solved pieces together. In a function, this manifests itself as having a base case, the smallest possible piece, and having the function call itself on a smaller piece until it gets to the base case, which is when it starts building back up. An example of a recursive function would be a function that calculates the nth fibonacci number. If you were asked to calculated the first or second fibonacci number, the answer would be simple: 0 or 1, because you already know what they were. However, if you were asked to calculate the third fibonacci number, then you would have to think back: what was the first and second number? Or, for the eighth fibonacci number, you would have to think: what are the sixth and seventh numbers? To solve that, you would need to find the fourth and fifth, and then the second and third, and so on. Hence, recursion is in fact a very natural way to think about solving certain problems, and usually leads to very readable, simple, code which can solve seemingly much more complex problems.

Using these two concepts of OOP and recursion, computer scientists are able to bring more natural concepts and ways of thinking from the real world into a program.

No comments:

Post a Comment