× Terms of Use Privacy Policy

Take me back!! I want my CSS!! Click me to get back :)

×

READY FOR A NEW CAREER?

But not sure where to start? Find out if a tech career is right for you.

TAKE THE 3-MINUTE QUIZ

Tech Term: Object-oriented programming

Object-orientation allows you to create objects within the world of your computer program that have specific and unique attributes and abilities.

When you think about programming languages the key thing to remember is that they are languages, just like human languages, and their purpose is to provide us, humans, with a way to communicate with them, the machines.

But since computers are extremely simple, programming languages also must be simple, and much of what we take for granted when speaking in our human languages must be made excruciatingly explicit.

Consider this situation: I have a computer program called pets in which I wish to have my dog Lola do doggy things like bark and fetch a stick.

Now, remember, the computer understands almost nothing until I explain it.

So, before I can tell the computer about my dog Lola and her fancy handshaking abilities, I have to explain to the computer what a dog is.

I would do this by creating the object Dog, and giving it certain attributes (name, breed, etc.) and certain abilities that are unique to Dogs (barking, fetching, etc).

In Ruby, one object-orientated programming language, the code might look like this:

class Dog
attr_accessor :name, :breed
def bark
puts "woof"
end
end

Now, with this code, I am able to create my dog, give her the name Lola, the breed Malti-Poo and make her bark:

mydog = Dog.new
mydog.name = "Lola"
mydog.breed = "Malti-Poo"
mydog.bark

When programming languages are not object-oriented then there are just objects and abilities, and no connection between the two. Meaning a dog can bark, but so could a blog post. The program has no way of knowing the difference between a dog and blog post.

If object-orientation makes perfect sense to you, that’s because we too are object-orientated; we understand that there are objects (dog, computer, boyfriend, tree) each with their own set of specific and unique attributes (breed, version, marriageability, deciduous or evergreen) and abilities (bark, program, kiss, defoliate).

And this is why Alan Kay’s creation of the first explicitly object-oriented programming language Smalltalk was so revolutionary: it brought computer programming one extremely important step closer to the human language.

Cocktail Party Fact

Ruby and Python, two very popular web programming languages are definitely object-oriented. And as of PHP v.5, PHP is also object oriented.

So the question remains: is JavaScript an object oriented programming language?

Apparently, that depends on how you define object orientation. Many people argue that because JavaScript does not support classes of object (as seen in the Ruby example above) it is not a truly object-oriented programming language.

Douglas Crockford, the programmer who created JSON, disagrees. He argues that JavaScript is one of the most grossly misunderstood programming languages and despite all the naysayers, is quite object-oriented!

What do you think?

Your email address will not be published.

8 comments

  1. Katy Exline Replied

    Thank you for explaining this! Can you go into a bit more detail and/or provide an example of what a non-object oriented language looks like and how it functions? Object-oriented programming DOES seem so intuitive, so I’m trying to grasp how languages that are not object-oriented work and their value.

    • Adda Replied

      This is a great question!

      So JavaScript is a good language to use as an example because even if it does do objects, it does them in a really different manner.

      When it comes to Ruby, you first define a class of objects (Dog) and then give it attributes & abilities.

      In JavaScript, you define Functions that act on objects, mostly HTML.

      For example, I might define a function slideShow and set it to a next & prev arrow, such that when you click the next & previous arrows you advance through the slideshow.

      That said, JavaScript doesn’t actually know what a slideshow IS or what the unique properties of a slideshow are (the fact that it has pictures & you can browse them in a linear format).

      As far as JavaScript is concerned it’s just following a set of instructions for how to do something, and if you asked it to do the same group of actions to a dog or a blog post or anything else, it would be like, sure, I can do that.

      On the other hand, if I defined a Dog and a Blog post in Ruby and gave them separate abilities (known as methods), if I then tried to make a blog post bark or a Dog publish, Ruby would be like, UM, what?! That don’t make no sense! Dogs don’t publish.

      • Katy Exline Replied

        Thanks for the response! So then when and why is it best to use object-oriented languages in some situations or contexts and not in others?

      • Sharon Cichelli Replied

        SQL is an example of a declarative, set-based language (not OO), which is really good at expressing intersections, unions, and aggregations (like sum or average). This makes it great for answering questions about a data set. (C# added LINQ extension methods to let you declaratively describe selections of collections; the syntax looks a lot like SQL. This is a non-OO part of C#. It’s not really that a language is or is not object-oriented, but that a block of code is or is not.)

      • Katy Exline Replied

        Aah! I’ve been trying to figure this out for a year and the two of you made it so much simpler. Thanks so much!

      • mucho Replied

        by your answer, it sounds like oop is less robust, as i cant use abbilities that arent attached to the object. and with the non oop, i can attach whatever ability i want, which seems better in the view of robustness of modularity. thats off course not the case, but i guess we have to exprience the coding ….

        anyway, it sounds from your answer that the main differnece of oop to non oop is the abbilities attachment options to objects?

        is that corrent? and if yes, can you elaborate more on why this is such a big deal (after all it seems that theres much more to it, otheriwise smalltalk-80 wouldnt be 800 pages almost)

      • stephenbooth Replied

        In OOP you do have to declare both the data (attributes) and abilities/actions (methods) of an object with that object (or rather with the class that object is one of). In non-OOP languages you declare variables, functions and procedures but don’t have any real connection between them.. This actually makes OOP more robust in most situations.

        In real world software projects you’ll often be working alongside other developers, maybe using libraries that have been bought in or existing code that previous developers have written. A surprisingly common problem, when using non-OOP is that you’ll use a name for a variable which another coder has already used. If you’re lucky the compiler will catch this and refuse to compile so you’ll have to go through your code, find each time you’ve used that variable and change its name. Compilers are quite good at this now bit weren’t always and when they do catch it the error message can be unclear. If you’re unlucky then the compiler won’t catch it and your code will give the wrong results because it’s picking up a different value to the one you were expecting. You can reduce the odds of this by using local variables as much as possible, but there will still be global variables, usually to hold the data that your program is actually doing the useful work on. You also cannot be sure that where the data has only certain valid values that it is valid. In the dog example you might have a variable called hungry which has valid values of ‘Not Really’, ‘Peckish’ and ‘Very’, you can’t guarantee that another coder might not replace ‘Very’ with ‘Ravenous’ somewhere in their code and so break your code that relies on that variable.

        In OOP attributes tend to be local and the need for global variables is reduced by being able to use objects (so instead of using a global variable myData you have myObject.myData). You also don’t tend to set attributes directly, you make them private to the object and use setter functions. Instead of “myDog.name = ‘Lola'” you have “mydog.setName(‘Lola’)”. You’ll also need a getter function to return the attribute when you need to do something with it (e.g. display it on the screen). This might seem to be extra coding, it is, but it does mean you can include your validation code in the setter function so you’ll know the variable will always be valid, or if it isn’t that the code causing the problem is within your object’s code so there’s a limited amount of code you have to check.

      • mucho Replied

        thanks for the detailed answer

Want more articles like this?

Sign up to get the most recent tech news, tips and career advice.