Tuesday, 8 October 2013

Blog Entry #2: OOP, Recursion

Blog Entry #2: OOP, Recursion

Oct 9th 



Object-Oriented Programming (OOP)

        Object-Oriented Programming is a type of programming in which programmers create a collection of objects to solve the problem.  An 'Object' can be anything, a person, a dog, a corporation, or even something more abstract like a menu on the computer interface. In terms of computer science, it is a software bundle of related state and behaviour that is used to model real-world objects in our everyday life. Programmers use a  class to create an object and define the types of operations (function) that is related to this class. In addition, programmers can create relationships between one object and another using inheritance. Some objects often share a certain amount of features with each other. For example, from exercise #3,  rectangle and rhombus are both a type of parallelogram, therefore we can use the same formula to calculate the area.  One great advantage of OOP is that it allows programmer to create modules that do not need to be rewritten when a new type of object is added. By simply creating a new object that inherits features from existing object, it saves work, time, and money in development and in testing. Also, when dealing to complex problems, it is natural and easier to solve them by breaking an design into objects. However, there are also a few inconvenience when using OOP to solve a problem. The data inside one object can only be manipulated by calling the object's functions. An OOP program is only permitted to use the data it owns, but cannot access shared data.

@Rahul Chaudhary (http://rhlcy.blogspot.ca) also talked about another OOP feature data hiding or abstraction in his blog. I think this is a really cool, and I'm definitely excited about trying it out in some other languages since it is not applicable to python.



Recursion

     Recursion is a computer programming technique that involves using a algorithm that repeatedly calls itself in order to solve the problem. To simply it, a recursive function is a function that calls itself and clones itself by making new copies of the code, local variables and the parameters. It is a widely used method in computer science.  So when and why do programmers use this technique to solve problems? As problems get more difficult to solve, the solutions tend to get bigger and longer. But if the problem can be broken down into small similar steps, it is possible to solve the problem by using the same procedure or algorithm at each step which leads to the final solution. Programers use this technique because it can be a great tool to solve some types of problems. Problems such as Tower of Hanoi and factorials are very challenging to solve using iterative solutions. In addition, the code of recursive functions are often much shorter, cleaner and simpler.  However,  a shorter program does not indicate improved efficiency. In fact, because the function is repeatedly called, and the computer system has to store all the data and return them after executing the new function,  recursive functions are usually slower and take more memory comparing to iteration.

PS. I do agree with @http://heapofcode.tumblr.com, one way to solve recursion problem is to work out the base case and use the same logic to solve more complex situations.