Presentation on theme: "Coding and Debugging. Requirements and Specification Recall the four steps of problem solving: Orient, Plan, Execute, Test Before you start the implementation."— Presentation transcript:
Coding and Debugging
Requirements and Specification Recall the four steps of problem solving: Orient, Plan, Execute, Test Before you start the implementation phase of your project (the Execute step), you should complete the prior two steps You will do this by creating a requirements and specification document; a sample is provided
Bugs Errors in a program are called “bugs” Don’t feel that your program should work right the first time. Almost every program starts out with bugs, even when written by experts The process of finding and correcting the errors is called “debugging”
Three Types of Errors 1 Syntax error: (VBA calls these compiler errors) This is a mistake in the form of your program. The VBA editor will catch these and turn the offending line red. It will also suggest ways to fix it. Usually the suggestions are right but they may not be. Run-time error: This causes your program to freeze up or crash while running. A well-written program shouldn’t do this, even if the user does something wrong. We’ll learn some methods to avoid these later.
Three Types of Errors (2) Logic errors: These are the most insidious errors, because it looks like the program is running correctly. Remember that the computer has no common sense and just does what you tell it to, no matter how silly. You use tests to see if you have logic errors, which is why the tests are of critical importance.
Stepping Through VBA has a great feature to let you see your program in action If you go to the code page, choose a subroutine to run (by clicking the title) and choose Step Into from the Debug menu (or use the F8 key on Windows, command-shift-I on the Mac; that’s the letter “i”), the computer will execute your program line by line The next line to be executed is highlighted in yellow Keep pressing F8 or command-shift-I to advance through the code After a line has executed, you can hover the mouse over the variable names and see their current values If you see a value you didn’t expect, you have found a bug Go to the Run menu and click Reset to change your code and try again
Compiling vs Interpreting When you step through your code, VBA looks at it and executes it line by line. This is called interpreting the code. Normally when you run your program, VBA compiles it, meaning it is translated into a form that runs quickly on your machine, similar to an exe file on Windows
Be Persistent Programming requires you to pay attention to detail Your program won’t run till every last syntax error is fixed Once it runs, you need to do your tests and track down any bugs The TA can help you if you get stuck. Or use the discussion groups on D2L