Programming, Objectively

Most of today’s most popular programming languages are object-oriented. Whether you’re writing in .NET, Java, or within the Apple realm (Objective-C and Swift), you’re writing in an object oriented language. And if you’re not writing a command line application, then you’re almost certainly using some amount of object oriented code for your user interface code, whether or not you realize it.

The great thing about object oriented code are these ideas of reusability, encapsulation, and inheritance.


Reusability

Object oriented programming means that we can write a class to do something very specific, and then when we need to do that specific thing again, we just reuse that class.  Consider adding a button onto a form or view.  We don’t have to write the code for a button from scratch each and every time.  There already exists a button class–we just need to get another button object.

Encapsulation

Object oriented programming means our objects can hide away some complicated logic–it’s stored away in a box.  At its core, every string object is a null-terminated array of characters–just as it was in C.  Instantiating a string meant allocating the right amount of memory, and resizing required some reallocation of that memory.  If you wanted to find the length, well you just counted elements until you found the null character.  Using the string class in an object oriented language makes all of this so much easier.

Inheritance

Object oriented programming means that developers can save a lot of development and debugging time if they’re properly using inheritance.  If you’re an absolute beginner to programming, object oriented programming can typically be a little hard to fully understand the consequences of for a long time.  If, for example, you’re developing a Windows desktop application, every time you need a new form, you’re just creating a new subclass of the form object.  It takes some experience to see that sometimes you need to inherit from your own forms to cut down on code duplication.

So, how do I learn object oriented programming?

If you’ve started writing in a language such as Java or C# where everything is an object, it can be tough to really fully learn the consequences of objects.  It’s important to understand the concept of instantiation.  Our class is nothing but a blueprint for an object.

Objects can be thought of as “things” or “nouns” if you will.  They can be thought of as the subject of a sentence.

Meanwhile, our instance variables can be thought of as the adjectives which modify our object.  That’s not just any ball, it’s a red ball.  The ball’s color property holds the value red.

And our instance methods should be though of as the predicates of our sentences.  For example, our ball class might have a kick method.  Any arguments the method takes might be thought of as adverbs.

When object oriented programming finally clicked in and made sense for me, it was after having implemented a Fraction class in Java, and for other beginner programmers I’ve talked to, implementing a Fraction class frequently helps them to gain a new level of understanding of object oriented programming as well.

Implementing a fraction class is useful, because it’s very easy to see why we would want to instantiate multiple fraction objects and manipulate them in various ways.  We’re already comfortable doing this with other types in programming, like integers or floating point numbers.  So the challenge is to implement a fration class which stores a numerator and a denominator as integers and can be used in all the same ways you’d expect to use any other type that represents numbers.

One of three things will happen if you take this challenge:

  1. You already know object oriented programming and will learn very little, if anything.
  2. You didn’t know object oriented programming, but completing this challenge helped you understand it significantly better.
  3. You still don’t quite get object oriented programming.

If you fall into the first category, this entire blog post was probably a waste of time for you.  This entry is entirely meant for OOP novices.

If you fall into the second category, that’s great.  You’ve learned a lot.  But I recommend, if you can, have a more experienced programmer look over what you’ve written.  You’re now at a new level of programming and will be more capable of understanding some more advanced concepts.

If you fall into the third category, don’t give up hope.  I still recommend you have a more experienced programmer take a look at what you’ve written if possible.  I’ve found that thinking about and talking about fractions and object oriented programming is very frequently a good gateway straight into truly understanding the basics of object oriented programming, and once you understand the basics, all the rest comes quite easily.

And if you don’t have access to experienced programmers willing to take a look at your code, I invite you to post your working examples to Code Review.  There are plenty of experienced users there (myself included) always willing to point out areas of improvement in code.

Leave a Reply