Let's see how two of your classmates use patterns.
Let's give Klaus and Jeremy a task. Write a program to convert pounds and ounces to kilos. It starts something like this:
Here's some output:
If the user enters data that isn't numeric:
If the user enters a negative number:
If the user doesn't enter anything:
OK, dudes… go!
The easy stuff
Option Explicit. If it isn't there, type it in.
Configuring Excel tells you how to do this. It's optional.
Speaking of code, let's put some in, dude. It's an IPO, right?
A good way to start. Pick the overall pattern. Then type in some comments, one for each chunk in the pattern. The comments are indented.
Integerfor the first two, and
Singlefor the other one.
Let's start on the input. That's the get-data-from-cells pattern, that we've seen before.
They can't type a negative, or something that's not a number. Do we know how to that?
Let's check the pattern catalog.
The pattern catalog has all of the patterns mentioned in the course.
The first test
How about we give it a try? See if the code works so far?
It's a Really Good Idea to write a little, test it, write some more, test it, etc. Don't wait until you write all of the code before you test anything.
Hey, dude, hit the debug button on that error thing. Maybe it will help.
pounds, the thing we want to check!
pounds. It's right here in the pattern.
The patterns have lots of good advice. Find what Klaus is talking about in the validating input pattern, before reading further. Go on, I'll wait.
userInputthing. Let's do that.
Wait… Oh dude! Duuuuude!
We put in code to test for negative, but in the spreadsheet, we put in text! That's a different test!
Man, am I glad we tested this now, instead of waiting until the end. Now we know what to do with ounces.
But let's finish the pounds test first. We've got the negative. Now we'll test whether the user entered a number.
Let me check the pattern… OK.
Testing-as-you-go is a Good Thing. Write a little, test a little, write a little, test a little…
Oh, dude! You saved us there. OK, let me flip them around.
Now negative… Worked too!
Ooo, how about typing in a regular number… Hey, no error message!
Dude! We got it!
This is true of all patterns. You need to adapt them to your situation. You can't do that if you don't understand them.
OK, let's copy that code we made for pounds, and use it for ounces.
That's the input. It's the hardest part of this, I'm guessing.
You're adding comments, too, saying what each piece o' code does. You're the Dude, dude!
Let's do us some tasty processing goodness, dude.
Adding comments helps explain the structure of the program. Adding a comment for each chunk is a Good Thing.
It says that 1 pound is 0.453592 kilos.
Oh, before you multiply, divide ounces by 16, and add it to pounds.
Oh, I get it. Sixteen ounces in a pound… OK.
All done… not!
We should have gotten:
But we got:
A natural reaction. Programming will make you crazy sometimes. Be comforted, though. Follow what you learn in this course, and you'll figure it out.
We'll go through one line at a time.
To debug, look at the variables. Compare the value they should have, with what the program actually gives them.
They work through the program a line at at time. They could work faster by starting somewhere in the middle of the program, but it doesn't matter for a short program like this.
poundsis 23. Dude, it lost the ounces. Like it never did the line that adds the ounces. It lost the part of a pound.
Oh. Let me check…
Oh, dude! Duuuuuuuuude! Dude!
Check it out.
Oh, dude! You rock!
pounds = pounds + ounces / 16
It drops the fraction! Duuuuude….
Dude! Yowser! It woooooorrrrrks, dude!
Dude, that was a lot of work. But we got it done.
Aye, programming is hard work. You've got to know how to thunk, how to debug… oh, and how computers work, too. But the last bit is the easiest.
Tara? Got a question.
Is that normal?
Klaus thought about the work he and Jeremy had done. That's a good idea. When you finish a task, look back on it. Anything you understand better now? Anything about the task or your work seem strange?
Programming is about the data. If the input isn't right, the calculations don't matter.
Geeks have a word for that. GIGO. Garbage in, garbage out.
Programming is hard work. That's one reason programmers are paid well.
Use patterns when you thunk. They help, by reminding you of things that work on similar problems.
However, you can't just cut-and-paste patterns. You need to understand them. Often you need to adjust them to the needs at hand.