Recursion is a method of solving a problem by firstly solving smaller instances of the same problem. In computer science, a recursive algorithm is one which calls on itself until a base case is reached. Recursion is very important for computer scientists, as it usually provides an elegant solution to many problems which could otherwise be very complicated to solve. For example, I often play chess and I recently found a problem which can be solved using a recursive algorithm. The problem is known as the "Eight Queens Puzzle". The problem is to place eight queens on a chessboard such that no queen can attack another queen. The recursive algorithm uses backtracking (once a conflict is found, the computer will go back to the last proper state and try to find a different combination). There are many other applications of recursion. This includes binary search, recursive data types (trees), fractals and much more!
Object-Oriented Programming (OOP) is also very important for computer scientists. An OOP language allows programmers to write classes which serve as blueprints, or templates for creating objects. This is very similar to real life. To build a radio, for example, a manufacturer would firstly create a template for the radio and then give this same set of instructions to a factory to mass produce. If the company wanted to make a different radio, then they would likely use a slightly altered version of the original template rather than constructing an entirely new template. This same idea is used in programming. Building a radio object would firstly require writing a class. Building a different radio object would require reusing the same class, but altering some of it (this is where inheritance comes in). This is highly advantageous for computer scientists as it does not require a lot of code to be repeated (much cleaner). Another advantage of OOP is information hiding (encapsulation). Going back to the radio example, an average consumer who buys a radio can use it and not worry about how the radio actually works as it comes with an interface (power button, volume control, etc.). This same advantage is present in computing. Programmers need not know how an object works, they just need to know that it does work (before OOP, coders needed to know how a piece of code works before using it). Clearly, OOP makes our lives easier when we are coding. It makes our code cleaner, reusable, readable and generally easier to work with.
No comments:
Post a Comment