I'm a proponent of Object Oriented Programming, and here's why.
Recently I started a
somewhat involved project that should make it much easier for me to
design web pages:
The last time I
worked a large project it became increasingly difficult to manage all
the code as the project grew. But as I’ve come to adopt classes
and objects it has become much easier to implement and manage code.
I’m use to writing
large chunks of code consisting mainly of variables, loops and
functions. I usually try to organize this code by separating
different types of functions into different files:
I still end up with
a mess of functions. Some functions only being used once. And a
mass of code I have to sift through to figure out how some variable
is being used (or misused).
One thing I love
about objects and classes is the organization. If I have a range of
variables associated to only one purpose then I can use an object
related to a purpose to store specific properties for very specific
functions. For example with mouse tracking I do something like:
My new “mouse”
object is it’s own nice little package with properties specific to
a given function. I don’t have to think too hard about what
variable I might be using for something. If it’s related to the
mouse then I know it’s probably a property within the “mouse”
object.
Sometimes when I’m
just trying to get something to work I’ll write a bunch of code to
accomplish a task. Once the code is working I’ll port it into a
class so I can drop it into my code as
a class whenever I need it, rather than try to figure out how to
insert a bunch of functions and variables without conflicting with
the code.
I recently did this
with an RSS feed banner. I wrote all the code to generate a small
banner with an image and top headlines from an RSS news feed.
And everything is
handled inside a single class that I attach to an element on the web
page:
This makes it a
no-brainer to implement code as classes. As long as I know what the
properties are for a class I can make the modifications I need and
just use it without thinking about it.
I guess I’m a big
proponent of objects and classes now. The irony is I use to have an
aversion to this type of programming. My thinking being “why do
they have to keep changing things”.
I know better now!
Comments
Post a Comment