Through most of our recent classes we’ve gone in depth with the concept of OOP (Object Oriented Programming). OOP allows a particular program to be broken down into many smaller modules or “objects”. 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. One of the biggest advantages of OOP is that it lends itself fantastically to teamwork. When various parts of a program are divided, it allows people to go off and work on a single part, and then in the end, come back and put it all together. In short, OOP allows programmers to abstract ideas from the real world and represent them in a program.
Another new concept we’ve gotten to interact with is of recursion been introduced into our course. 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. Without this concept, a lot of programming problems can be unnecessarily difficult. Initially I found the concept a little intimidating however I feel by now I’ve comparatively gotten more comfortable with the concept. I’ve reached a stage where I enjoy playing around with it and find new ways to implement it.
An example of a recursive function would be a function that calculates the nth fibonacci number. If you were asked to find the eighth fibonacci number, you’d have to find out the sixth and seventh numbers? In order for that, you’d have to calculate the fourth and fifth, and then the second and third, and so on. By the end of its implementation we end up with a simple and short solution, as opposed to the mountain of complications a non-recursive solution would entail.
One of the basic uses of recursion can be found in trees. A tree will have some value, and would further have children that are trees with their own children, which are trees, and so on. To find a certain value within a tree, you’d go through the following steps
Look for the search value in this tree’s value.
If you haven’t found the value and the tree has children, look for it in this tree’s children. If there are no children, the value is not in this tree.
Hence, recursion usually leads to very readable, simple, code which can solve seemingly much more complex problems.