Object Oriented Programming.
Or should I say 'Programming in a style that is consistent with objects being the most important thing in a class like structure'.
In a nutshell, Object Oriented Programming can take a very simple problem like coding the game Subtract Square and make it into many hundreds of lines of code with a smorgasbord of classes. Thus when faced with a simple task, but adjoined with the footnote to use Object Oriented Programming, causes a lot of sighs from the general crowd of computer programmers.
However the benefits of Object Oriented Programming are quite robust and necessary for doing anything remotely complex in a readable way. Readability of code is a necessity, both for the programmer and anyone attempting to figure out what the code actually does. Naming conventions are very important and the layout makes everything more obvious, but to program with classes and effectively use __str__ and the like well, turns good code into great code.
The isolation of specific tasks into distinct methods in an overarching Class that deals with a certain type of object not only allows a reader to figure out what each call does, but allows a programmer to easily write each function and test it with an appropriate docstring. The use of docstrings in methods of Classes is my favourite way to construct code.
My old computer science teacher from high school had a saying which has stuck with me to this very day. He called it prematurely coding, writing code before you knew exactly what you wanted to occur and how you wanted it to occur. With a class of grade 10 computer science students, prematurely coding was a regular occurrence. Knowing you needed to go through every item in a list, and writing a for loop that does that before even considering what you're going to do with each item, or what data structure is necessary to facilitate one's calculations.
After assigning anything, he'd always tell us to write it out, to make a diagram, to actually do the math that was behind it all. Many students didn't take his advice at all. Most would end up with unreadable dysfunctional code, and a lucky few would end up with unreadable, convoluted, slow but working code.
Those who chose to follow his advice would eventually figure out that each problem he assigned didn't have to be solved with brute force but there was an elegant solution that could be coded in less than 10 lines, or with two simple helper functions.
A very memorable example of this was a tricky assignment from the middle of the year. It was simple stated as follows:
"Given Point A, Point B and Point C on a cartesian 2-Dimensional plane, write a function that returns true iff Point D lies within the triangle ABC."
I've always found with math and computer science, the simplest put questions are some of the hardest to solve. This challenge turned out to be no easy task for those who began to prematurely code. They did their best to find the equation of each of the lines AB, BC and AC and then determine the position of Point D in relation to each line. After writing several hundred lines of confusing code, it turned out that none of them could actually get the correct output for my teacher's testing cases.
Conveniently we had a field trip planned for the week after this assignment was due and we traveled to the work place of my best friend's dad, who happened to design software. He worked for Side Effects Software Inc., the company responsible for writing Houdini, a 3D animation software which was used to generate the strings on the balloons in up, along with the process of transforming transformers... the list goes on and on.
The field trip was a lot of fun as we got to listen to various programmers talk about working in the industry. For example their fun policy of after a days work, if anyone breaks the code that they're collectively working on, they have to bring in donuts for everyone the next day. This rule was gleefully told to us by a programmer who purposefully broke the code each day and fixed it right before he left so no one else could figure out if their changes worked or not, thus increasing the amount of donuts people brought him.
The important part of the field trip was when our teacher prompted my friends dad to talk about points inside of triangles. He told us that figuring out if a point is inside a triangle is a very important part of 3D modelling because every surface is created by tiny tiny triangles.
In the end the solution took a very interesting look at the concept of being inside a triangle by dividing the shape up into three more triangles: ABD, ACD and BCD and comparing the sum of their areas to the area of ABC. If the areas are equal, then D is inside the triangle ABC. If the sum of the areas is large than the area of ABC then the point is outside of ABC. This neat geometry trick is obvious when drawn on paper, and requires a formula that is very mathematical in nature. Given three points in a two dimensional cartesian plane, what is the area of the triangle defined by those two points.
After figuring out the process, and getting our hands on the equation, writing the code to solve the given problem was easy and every finished it in a single class time.
At Side Effects Software Inc. they have a method in their code that checks if a point is in a triangle, since they use this so often. Their use of Object Oriented Programming allows for more elegant code that comes together in an overarching design.
In the end, I've grown to love Object Oriented Programming as it makes things elegant and make sense. Creating a good program design requires a lot of programming before even touching a keyboard, and I do this best with flow charts. The file game_view in Assignment 2 can be represented by quite a complicated flow chart, but one that makes a lot of sense in the order and how things are executed. Hence I always create a flow chart when designing a program so that I correctly program it in an Object Oriented way.
No comments:
Post a Comment