Rails Mama’s Guide to Object Oriented Programming Part 6: Data Types

We’ve talked about 2 of the 3 “Pillars of Object Oriented Programming”: Encapsulation and Inheritance. Before we can talk about the third, “Polymorphism”, it helps to understand Data Types.


This is part 6 of a series. To get the most out of these posts, read them in order:

Part 1: Objects
Part 2: Classes
Part 3: Multiple Inheritance
Part 4: Recap & Quiz

Part 5: Encapsulation


Part 6: Data Types

My favorite analogy for Data Types comes from a super-duper-academic article written in 1985. It’s a great article, but I’ll give you the playful, non-academic version:

Once upon a time, all the data in the world was naked. It frolicked around in the Garden of Computer Science, with nary a leaf to cover it’s unmentionables. It was naked and free, and totally unprotected from poison ivy.

As the Data population grew, it become more and more dangerous for Data to run around naked. It was one thing for Data to frolic around alone in a peaceful garden, but if it tried walking into a busy New York subway that way….well, let’s just say it might get misused.

To protect their Data, programmers began to send it out into the world dressed in Data Types.

Much like clothes, Data Types make a statement about what to expect from Data, and also make it less likely for that Data to be used in an inappropriate way.

Picture a girl in with a pink, frilly dress and a tiara. She is making the statement “I am a princess.” She is also sending a message about what would be an appropriate or inappropriate way to interact with her.

Appropriate: “Would you like some tea, your Highness?”

Inappropriate: “Let’s jump in that mud pit and wrestle some pigs!”

princess_mud

Here are a few of the Data Types you’re likely to find in your programming language’s wardrobe:

String: Says “Treat me like a word or sentence”.

  • Appropriate things to ask of a String: “Print yourself.” “Join with this other string to make a longer string.”
  • Inappropriate things to ask of a String: “Divide yourself by 3 and tell me what the remainder is”.

Boolean: Says “I am either True or False. There is no gray area here.”

  • Appropriate: “Tell me if you’re true or false”.
  • Inappropriate: “Multiply yourself times 10.”

Integer (or Fixnum): Says “I am a nice, round number with no decimal places.”

  • Appropriate: “Tell me if you are larger or smaller than the number 14.” “Multiply yourself times 20.”
  • Possibly Inappropriate: “Do some division and give me a precise answer.” (This is a tricky one. Some languages, such as Ruby, will take a problem like 3/2 and say “Hey, they gave me 2 Integers, so they must want an Integer back.” Since the answer is 1.5, and that is NOT an Integer, it has to be either rounded up or rounded down, so you get a silly answer like 3/2 = 1.)

Float: Says “I am not so pretty and round, but at least I know what 3 divided by 2 is.”

  • Appropriate: “Divide yourself by 2 and give me a precise answer.”
  • Inappropriate: “Tell me if you’re true or false.”

There are many other Data Types, and different languages use different ones, but this should illustrate their purpose. Data Types are just a wrapper around your data, which tells the program what to expect of it, and how it can and cannot be used.


 

Ready to Learn More? Check out Part 7: Polymorphism

Advertisements

2 thoughts on “Rails Mama’s Guide to Object Oriented Programming Part 6: Data Types

  1. Pingback: Rails Mama’s Guide to Object Oriented Programming Part 5: Encapsulation | Rails Mama
  2. Pingback: Rails Mama’s Guide to Object Oriented Programming Part 7: Polymorphism | Rails Mama

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s