Avoiding bugs

Log in

If you're a student, please log in, so you can get the most from this page.

In the last lesson, you saw how to use the debugger, and how to use Option Explicit to avoid bugs in the first place. Let's talk about some more ways to avoid bugs. The fewer bugs there are to begin with, the happier you'll be.

Incremental testing

When you have lots of code to test, bugs could hide anywhere. The solution? Don't have lots of code to test!

Write code in baby steps. Write a few lines, test, write a few more, test. When you find a bug, it will probably be in the last few lines you wrote.

Do this from now on.

Principle

Incremental testing

Write a little code, then test it. Write a little more code, then test it.

Meaningful variable names

Check this out.

  • x36 = ph3t3 + ph3t3 + y33a
Reflect

Does that code look right?

(If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.)
Adela
Adela

Who knows? x36? y33a? Could mean anything.

How about this code?

  • sTotalPrice = sPrice + sPrice + sSalesTaxRate
Reflect

Does that code look right?

(If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.)
Ray
Ray

Well, no. Looks like the second + should be a *. It should be:

  • sTotalPrice = sPrice + sPrice * sSalesTaxRate

Right. Because you know how sales tax works, you could see the problem.

Using meaningful variable names makes your code easier to write, easier to read, and easier to fix. From now on, make sure you use meaningful variable names for all of your exercises.

Hungarian naming

Also, let's use Hungarian naming for variables. The first character (or two; more later) tells you about the data type, like s for Single variables. As you learn about more data types, you'll see different characters. Knowing the types will help a lot when it comes to validation.

After the data type letter(s), use one or more words. The first character of each word is uppercase, like sGoatHappinessLevel.

Use names that explain what the variable is. Long names are OK. For example, instead of sGtHap, use sGoatHappinessLevel.

We've already been doing this, and it seemed natural. "Total price" is sTotalPrice. "Sales tax rate" is sSalesTaxRate. Price is just sPrice. You knew what these things were, even though you didn't know the Hungarian naming standard.

Pattern

Hungarian variable names

"Sales tax rate" is sSalesTaxRate. Price is just sPrice.

Ctrl+Space, your superfriend

You don't have to type in the full name of a variable when you're writing code. Suppose you have a variable called sAverage, and want to output it into a cell. You type the first few characters of the variable name:

Type first few characters

Then hit Ctrl+Space.

List of symbols

You'll see a list of the symbols that start with those characters.

There are several things that start with the letters you typed. The one you want is the third in the list. Use the down arrow to choose the one you want.

Use down arrow

Press Tab. VBE will finish the variable name for you.

Finished

Faster, and fewer spelling errors. M0000t!

Indentation

Later, we'll talk about if statements and loops. Still, here's some code that should make sense.

In retail, some buyers are exempt from state sales tax. Maybe churches, in some states. Some items are exempt from sales tax, like food.

  1. sBasePrice = sUnitPrice * sNumberUnits
  2. If tExempt = "yes" then
  3.   sTotalPrice = sBasePrice
  4. Else
  5.   sTotalPrice = sBasePrice + sBasePrice * sSalesTaxRate
  6. End If

Lines 3 and 5 depend on line 2. If the buyer or the product is tax exempt, line 3 runs. Otherwise line 5 runs. Only one of them runs.

Lines 3 and 5 are indented, that is, pushed to the right (press the Tab key in VBE to indent). That makes it easy to see that they depend on another line.

Here's the same code without the indenting.

  1. sBasePrice = sUnitPrice * sNumberUnits
  2. If tExempt = "yes" then
  3. sTotalPrice = sBasePrice
  4. Else
  5. sTotalPrice = sBasePrice + sBasePrice * sSalesTaxRate
  6. End If

It runs the same. VBA doesn't care about indenting. But it's a harder for us humans to work out what's going on. That means more bugs, more time, and more frustration.

So, indent your code.

Comments

Some code:

  • sVolume = 3.14159 * r * r * h / 4
Reflect

Does that code look right?

(If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.)
Adela
Adela

It would be easier to tell if we knew what the formula was for.

Fair enough. Let me add a comment.

  • ' Compute volume of a cone.
  • sVolume = 3.14159 * r * r * h / 4
Adela
Adela

Now we know what to google. Let me check... No, the last 4 should be a 3.

You're right!

Comments are notes for people. Computers ignore them. Comments explain what code is supposed to do. They're helpful when debugging programs, or changing what they do.

Adela
Adela

Changing what they do?

Yes. Business changes all the time, so the programs that help businesses run have to change as well. Most programmers spend more time changing existing code, than writing new programs.

The easier code is for programmers to understand, the faster and more accurate they can work. Comments are a big help.

The harder it is for you to figure something out, the more important comments are. If it was hard for you when writing the code, it will be hard for someone else to understand what you did. Comments make it easier.

Truth goat

​Truth goat: Comments are your friends.

True dat, TG. True dat.

Avoiding bugs

We've talked about:

  • Using Option Explicit
  • Incremental testing
  • Meaningful variable names
  • Hungarian naming
  • Ctrl+Space
  • Indenting
  • Comments

Exercise

Exercise

Yucky code

Download a workbook with poorly formatted code. It's for computing income tax adjustments, using the following rules:

  • Base tax is 22% of income
  • Each child adds $3,000 to tax. Children are a bad idea, in today's world.
  • Each dog reduces tax by $1,100.
  • Each cat reduces tax by $300. Cats are OK, but dogs are better.
  • Attractiveness ranges from 5 (stunning) to -5 (aesthetically challenged). Multiply the rating by $2,500 to get the change to tax.

Improve the code.

  • Fix the variable names.
  • Rearrange the code, to match IPO.
  • Add comments describing each calculation.
  • Add anything else that's missing.
  • Fix any bugs you find. E.g., where's the base tax?

Upload your solution.