Thursday, 23 January 2014

Object-Oriented Programming

Object-oriented programming is confusing.

When I first learned programming (around Grade 6) I used Pascal and it complemented my thinking process of how a program should work: line by line.  Procedural programming focusing on the ability to manipulate an output utilizing functions was much more coherent to me.  Take a variable, do this to it.  Then do this.  Then do that.  This type of programming also helped me debug code a lot easier.  Set up a breakpoint, set up variable watches, and then go through line by line and immediately pinpoint where the code is messing up.

I was also taught computer-type mathematics at the same time - recursion algorithms, regular expressions, graph theory, basic boolean algebra, and LISP.  These concepts reinforced the procedural thinking process - operations working on variables taken one at a time.  None of this silly object business.

Then in Grade 10 they told me to learn Java and thus I approached it the Pascal way.  Only to find out that it wouldn't work.  Now functions and procedures are called "methods" and they only operate on certain types of things that you have to make yourself called "objects".   And you have to define a pile of initialization code and work around a pile of exceptions.

What happened to all those nice "I can use this function on everything" programs?

With that in mind, I find that it's a lot easier to think about programs in terms of procedures.  If thinking about an object is required, then it's about thinking of the procedures an object follows, not the attributes that an object contains and how to manipulate them.  Learning to figure out what code should DO instead of what code should REPRESENT is quite a bit more important and really benefits me in terms of thinking of algorithms.

Now that I've learned Python, which has an even looser system of variable declaration and function formation, I'm finding myself very grateful that procedural programming has taught me how to think about coding in an object-based system.  An object can easily be summarized as a series of procedures to work on variables that are assigned to it, making each a self-sufficient "program".  The main program just tells the object program what to do.

A different perspective on object-oriented programming makes it a little less confusing.