15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

44<br />

Organizing a Program with Object-Oriented Programming<br />

Object-oriented <strong>programming</strong> solves two glaring problems with structured<br />

<strong>programming</strong>: reusability and modeling.<br />

Reusability means that you can collect smaller programs that work together,<br />

store them in a larger group called an object, and then plug those objects into<br />

different programs like Lego building blocks. Where structured <strong>programming</strong><br />

encourages reusability by letting you reuse subprograms, object-oriented <strong>programming</strong><br />

encourages reusability on a larger scale by letting you reuse objects<br />

(which contain multiple smaller programs). Reusing individual subprograms is<br />

like using bricks to build a house. Reusing objects is more like using premanufactured<br />

walls to build a house.<br />

Modeling means that <strong>programming</strong> is more intuitive. One of the reasons why<br />

assembly language is so hard to understand is because manipulating data in<br />

the processor’s registers has nothing to do with solving problems like adding<br />

two numbers together. Likewise, dividing a large program into smaller tasks,<br />

using structured <strong>programming</strong>, does nothing to help you understand the<br />

actual problem the program is trying to solve.<br />

For example, suppose you had to write a program to land a rocket on the moon.<br />

This is how you might write this program using structured <strong>programming</strong>:<br />

Land a rocket on the moon<br />

Launch rocket<br />

Guide rocket through space<br />

Find a landing area on the moon<br />

Put rocket down on landing area<br />

So far, structured <strong>programming</strong> seems logical, but what happens when you<br />

keep dividing tasks into smaller tasks? Just focusing on the Guide rocket<br />

through space task, we might wind up with the following:<br />

Guide rocket through space<br />

Get current coordinates<br />

Compare current coordinates with moon coordinates<br />

Adjust direction<br />

Dividing the Adjust direction task into smaller tasks, we might get this:<br />

Adjust direction<br />

Identify current speed and direction<br />

Determine angle needed to steer towards the moon<br />

Fire thrusters to change the angle of the rocket<br />

Notice that the deeper you keep dividing tasks, the more removed you get from<br />

knowing what the main purpose of the program may be. Just by looking at the<br />

task Identify current speed and direction, you have no idea whether

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!