Dudes at work

Let’s see how two of your classmates use patterns.

The task

Let’s give Klaus and Jeremy a task. Write a program to convert pounds and ounces to kilos. It starts something like this:

Start

Here’s some output:

Output

If the user enters data that isn’t numeric:

Characters

If the user enters a negative number:

Input error

If the user doesn’t enter anything:

Input error

OK, dudes… go!

The easy stuff

Klaus
Klaus
Dude, let’s start with the easy stuff. Typing the labels into the worksheet, and making the Convert button.
Jeremy
Jeremy
OK, no problem, dude… (clickity clickity typeity typeity)… there.

The easy stuff

Notice Option Explicit. If it isn’t there, type it in.

Jeremy
Jeremy
Hey, look, dude. The worksheet columns use numbers instead of letters. Makes code easier to write.

Configuring Excel tells you how to do this. It’s optional.

IPO

Klaus
Klaus
Oh, yeah! I hadn’t noticed.

Speaking of code, let’s put some in, dude. It’s an IPO, right?

Jeremy
Jeremy
Yep, looks like it. i’ll type in the comments, for each chunk of that pattern.

IPO

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.

Klaus
Klaus
For VBA, there’s a declare variables chunk, too.
Jeremy
Jeremy
Oh, right, dude!

Declaration comment

Jeremy
Jeremy
Hmmm… usually we have a variable for each of the input cells, and each of the output cells. I’ll put those in.

Variables

Klaus
Klaus
Are they the right data types?
Jeremy
Jeremy
Yeah, I think so. Have a look at the output screen shot we have.

Output

Klaus
Klaus
Oh, I see what you mean, dude. Pounds and ounces are both whole numbers, but kilos is not. Makes sense to use Integer for the first two, and Single for the other one.

Let’s start on the input. That’s the get-data-from-cells pattern, that we’ve seen before.

Pattern

Input from worksheet cells – known amount of data

Situation:
Your program needs input from the user. You know exactly how many elements you need, and what type each one is. The tip program is an example.
Actions:
Choose a few cells on a worksheet. Tell users what data to put in the cells.

Jeremy
Jeremy
(Clickity clickity typeity typeity)… OK.

Input

Klaus
Klaus
Dude, we need to check what the user typed in the cells, right?

They can’t type a negative, or something that’s not a number. Do we know how to that?

Jeremy
Jeremy
I don’t remember.

Let’s check the pattern catalog.

The pattern catalog has all of the patterns mentioned in the course.

Klaus
Klaus
Oh, yeah, this one! Input validation.
Jeremy
Jeremy
Yeah, dude, that looks right! Let’s try it. Let’s test whether pounds is less than zero.
Validation

The first test

Klaus
Klaus
Looks right to me!

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.

Validation

Validation

Jeremy
Jeremy
Dude.
Klaus
Klaus
Ack!

Hey, dude, hit the debug button on that error thing. Maybe it will help.

Validation

Jeremy
Jeremy
But that’s pounds, the thing we want to check!
Klaus
Klaus
Let’s check the pattern again… Dude, we should have read the whole thing. We need to check what’s in the cell before we put it in 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.

Jeremy
Jeremy
Oh, yeah. Hey, I like that userInput thing. Let’s do that.

Validation

Validation

Klaus
Klaus
Argh!

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!

Validation

Jeremy
Jeremy
Ay caramba! You’re right! OK, let’s make the error match the test, and see if the test picks it up.

Validation

Validation

Jeremy
Jeremy
Dude! It worked!

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…

Validation

Klaus
Klaus
Dude, wait! The pattern says to put the numeric test first… Yeah, here it is. Or you’ll still trying to put a string into a number.

Jeremy
Jeremy
Huh? Let me check…

Oh, dude! You saved us there. OK, let me flip them around.

Validation

Klaus
Klaus
OK, let’s try it with text for pounds… Worked!

Now negative… Worked too!

Ooo, how about typing in a regular number… Hey, no error message!

Dude! We got it!

Klaus and Jeremy used the input validation pattern. They started off trying to use it blindly, just cutting-and-pasting code. It didn’t work. They had to understand the pattern before they could use it effectively.

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.

Jeremy
Jeremy
We are awesome!

OK, let’s copy that code we made for pounds, and use it for ounces.

Validation

Klaus
Klaus
Let’s try it out… Yeeeeeeeha!

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.

Processing

Jeremy
Jeremy
OK, let’s see. I’ll ask Google about converting pounds to kilos.

It says that 1 pound is 0.453592 kilos.

Klaus
Klaus
Makes sense that it’s less than 1. A kilo is more than a pound, so multiplying pounds by 0.4 will give you a lower number.

Oh, before you multiply, divide ounces by 16, and add it to pounds.

Jeremy
Jeremy
What?

Oh, I get it. Sixteen ounces in a pound… OK.

Calculating

Klaus
Klaus
Only the output left. Check the exercise… hey, kilos should be to three decimals. Do you know how?
Jeremy
Jeremy
Yeah, dude, I remember that one. The Round() thing.

Rounding

All done… not!

Klaus
Klaus
Let’s try it. I’ll put in the numbers from the example in the exercise… DUDE! It didn’t work! The kilos is wrong.

We should have gotten:

Right

But we got:

Wrong

Jeremy
Jeremy
Whaaaaat?! Ah, dude, computers suck!

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.

Klaus
Klaus
OK, let’s use the debugger. How do you want to do it?

Jeremy
Jeremy
Let’s check all the variables as the program does stuff. I’ll put a breakpoint on the first line. Well, the first one I can.

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.

Breakpoint

Jeremy
Jeremy
There’s the breakpoint. Now run it.

Stopped at breakpoint

Jeremy
Jeremy
OK, press F8 to step to the next line. Let’s see what userInput is.

Stopped

Jeremy
Jeremy
OK, that’s right. F8 again.

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.

Time passes…

Wrong value

Klaus
Klaus
Wait, what? pounds is 23. Dude, it lost the ounces. Like it never did the line that adds the ounces. It lost the part of a pound.

Jeremy
Jeremy
Yeah, that’s crazy, dude! Where did that…

Oh. Let me check…

Oh, dude! Duuuuuuuuude! Dude!

Check it out.

Declarations

Jeremy
Jeremy
We made pounds an integer.

Klaus
Klaus
Yeah, so what? Wait…

Oh, dude! You rock!

pounds = pounds + ounces / 16

It drops the fraction! Duuuuude….

Jeremy
Jeremy
Let’s make them Singles, dude!

Singles

Jeremy
Jeremy
Give it a try…

Dude! Yowser! It woooooorrrrrks, dude!

Klaus
Klaus
Ay caramba! We did it!

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.

Thinking back

Klaus
Klaus
Dude, I gotta ask Tara something.

Tara? Got a question.

Tara
Tara
Sup?
Klaus
Klaus
Our program. Almost all of it was validation. Calculation and output was just a few lines.

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?

Tara
Tara
Yes, that’s normal, Klaus. Especially when people type stuff in. Easy to make mistakes. I mistype stuff all the time.

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.

Jeremy
Jeremy
Ha! Nice word. GIGO.

Summary

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.