15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

46<br />

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

Suppose you wrote a program to calculate a rocket’s trajectory to the moon,<br />

and the engineers suddenly designed the rocket with a more powerful engine?<br />

With object-oriented <strong>programming</strong>, you could just yank the engine object<br />

out of your program, rewrite or replace it, and plug it back into the program<br />

again.<br />

In structured <strong>programming</strong>, modifying the program to reflect a new rocket<br />

engine would mean finding the program commands that manipulate the data<br />

that represents the engine’s thrust, and then making sure that new data gets<br />

fed into the program at the proper location and still works with any other<br />

program commands that also handle that same data. (If the explanation in<br />

this paragraph sounded confusing and convoluted to you, that just shows<br />

you the less-than-intuitive problem of modifying a structured program<br />

versus an object-oriented program.)<br />

Objects simplify modifications<br />

Besides organizing a large program into logical pieces, objects have another<br />

purpose — code reusability. Just as in high school, it was always easier to<br />

copy someone else’s homework rather than do it yourself, so programmers<br />

find that it’s easier to copy and reuse somebody else’s program rather than<br />

write their own from scratch.<br />

In structured <strong>programming</strong>, you could divide a large program into subprograms<br />

and then store those subprograms in a separate file. Now you could<br />

copy that file to reuse those subprograms in another program.<br />

Copying subprograms makes <strong>programming</strong> easier, but here are two problems:<br />

✦ What if you copy a subprogram and then later find an error in that<br />

subprogram? Now you’ll have to fix that subprogram in every copy. If<br />

you made 17 copies of a subprogram, you’ll have to fix the same error 17<br />

times in 17 different copies of the same subprogram.<br />

✦ What if you want to modify and improve a subprogram? Suppose you<br />

find a subprogram that asks the user to type in a password of no more<br />

than 10 characters, but you want your program to allow users to type in<br />

passwords up to 25 characters. At this point, you could either<br />

• Write your own password-verifying subprogram from scratch<br />

(which would take time).<br />

• Copy the existing subprogram and modify it (which would take<br />

much less time). It’s easier to make a copy of an existing subprogram<br />

and then modify this copy. Now you’ll have two copies of (almost) the<br />

same subprogram, but uh oh! Suddenly, you discover an error in<br />

the original subprogram. Once again, you have to correct this error

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

Saved successfully!

Ooh no, something went wrong!