Programming language Visual Basic 6 (Chapter IX - Debug (Debugging and error handling))

Bugs are the fault of the program which I discovered when running it. Debug is the work of all types of faults in the program so that it runs smoothly in all circumstances.

Usually want to fix a bug that we must find out why it occurs. Once you know the proper justification, then we'll figure out a solution. Generally, there are two kinds of bugs:
  1. Either the program does not do the right thing to do because the programmer misunderstood Specifications  or given false information or omitted information program is required. We resolve this case by reducing misunderstandings through upgrading communication capabilities.
  2. Program fails to comply programmer as you want. That is a decent programmer but want to do a self insurance program since unknowingly not write properly programmed. This case is solved by using the Software Tools (including programming languages) suitable, and have the system working.
In it, people use a car from  Quality Control  to say regardless of the car has no faults at all. To achieve that, not only need people that the quality control personnel carefully assembled to the main task of quality control is confirmed good results and not finding fault.

Many factors affect the quality of a program such as program functions, the structure of parts, technical programming and debugging methods. Debug is not necessarily located at the end of the project which depends heavily on the factors mentioned first in all stages of development.

Function of the program (Program Specifications)

Oil and program large or small, we must confirm it clearly and meticulously what they should do, how many users, the network how much larger database, how to run fast. Etc..
There are many programs that have been changed again as long as Programmers misunderstood what customers want. Suffering is as close to delivery discovered some weaknesses in the program on the one hand we want to do a fall. Therefore in our relationship with our clients we need to ask, ask, respond to customers several times what we understand by correspondence, documents, to acknowledge that we knew before they wish to promote the program design. If the client later changed his mind, that is their right, but they have to pay changes ( variation ).

Structural parts

Program which also has an architecture similar to a home. Each division as simple as possible and how to assemble the parts to me how easy try. While designers have to know the weaknesses of each department is where we prepare the way to try them. I will not believe how perfect parts until tried it, although it is simple to first.

If we want to use a technique in what circumstances we are not sure it should not try to run it separately first. The method is called  Prototype .

Also, we should also plan for the unexpected cases, typically bad data - when the user clicks haphazard or database containing sewage.

If the program runs in  real-time  (ie, income data through Serial Comm Port, or Network Card Data Acquisition), you should note the different cases, depending on what happens before, what happened next.Logic at the time of the program will depend on the state ( State ) of data. It is best to think of the Scenarios  (proceedings of the circumstances) to be able to try every stage and situation.

Today's technology, Object at design stage as this is the decision of Data Structures (tables, records, etc. ..) and figure Forms with Classes. Remember that each class consists of a Data Structure and Subs / Functions / Properties work (thao) on that data. Data structure must contain sufficient information (data fields, variables) we need. Next was the way the program processes data. Subs / Functions that may call for outside, then we give it  public , but the Subs / Functions exist to serve others within the class, then we give it  Private .

Programming techniques

Programmers and the basics of their routines are important. Overall, those who rush, jump into the program before writing or thinking, then definitely consider the following bugs bugs everywhere this is something natural.

Dùng Subs và Functions

If the architectural design phase of the program is divided by class, when we re-design programming details of Subs, Functions. Etc., Each will need to test how. If we can divide the work into each stage, each stage may be that a call to a  Sub . What things should work out or taken from elsewhere, may be implemented by a  function .

Eg work in a laundry could include the following:
  1. Consignee
  2. Dividing each
  3. Bleach
  4. Laundry
  5. Board
  6. Sack
  7. Checkout
  8. Delivery
In which the steps 1,2,6 and 8 may be the Subs. But those steps 3,4,5 and 7 Functions, such as when we assign  Function Washing  a dirty shirt will get a clean shirt.

Remember that the difference between a Sub and a Function is a function gives us a result that does not change the  parameters  we gave it. Meanwhile, the oil that does not give us what Sub explicitly but it can change the value (value) of any parameters that you pass it  ByRef . That when we pass a parameter ByVal  for a sub is like we put a  copy  (copies) of that variable for Sub, Sub can modify it but it will be ignored, does not affect the  original  (copies main) variable.
Conversely when we pass a parameter  ByRef  to a Sub is like we put the original variable to the sub so it can modify it.

Thus in order to avoid accidentally make a variable value was changed because we use it in a Sub / Function should use ByVal when you pass it as a parameter to a Sub / Function.

In fact, you can use ByRef to pass a parameter to a function. In that case, of course, that variable can be modified. This is called a side effect ( side effect ), because few would normally do so. Therefore, if you really want beyond the ordinary rules should clearly Comments to warn people to read your program later.

In addition, each programmer usually has a  Source Code Library  of Subs / Functions like that. You should use the Subs / Functions in the Library of you as much as possible, as they have been tested already.

Do not be afraid Error

Every program has an error, or a  Compilation Error  (because we write code is not correct grammar, vocabulary), or the error while running the program, then you should not fear it. Calmly reading the  Error Message  to see what it meant. If you do not understand immediately, read a few times and see if there are heuristics to find any hints. Craft of programming as we will see Errors eat dinner, so you must set opposite to them calmly.

Use Comments (Note)

Remember when writing code to add  Comments  to fully back any time to read the code in the future that you do not need to rely on any other material that can immediately understand the purpose of a Sub / Function or code.
So you do not necessarily have to write a lot, but everyone can point Comments unusual, mysterious, then you need to inform and explain why you made this way. Maybe later we discovered the code has bugs, as readers can see the oil that we will design intent and how to program correctly, but is somewhat lacking such control.

Calculate the average programmer work only 18 months in each place. That is, almost certainly code you write will be others to read and maintain (debug, and more less). Therefore, the code be as simple and straightforward as possible. Do not worry that the program will run slowly or takes up more memory, because today's computer memory to run very fast and very cheap. When do we really need to care about speed and memory, it should be designed carefully and not rely on the trick of programming.

Naming the variables significantly

Suffering is working with variables named briefly as K, L, AA, XY. I do not have a little idea who they are, exist to do. Instead, if we put the names variables as NumberOfItems, PricePerUnit, Discount. Etc. it will be easier to understand.

One of the most difficult bugs that we use the same names for  local variables  (variables declared in the Sub / Function) and the  global variable  (or variable declared in the Basic Forms Module). Local variable will cover the same global variable name, so if you want to talk to in the context of global variable that will be used as local variable.

Dùng Option Explicit

You should be faithful to use  Option Explicit  at the top of each form, class or module. If there are any spelling variable order VB6 IDE will tell you immediately. If you're not using Option Explicit, misspell a variable is seen as a new variable with a value of 0 or "" (empty string).

Generally you should be cautious when assign a data type for a variable to another data type. You must know what you are doing to avoid side effects (side effect).

Desk Check

Verify code before compiling. When we compile the code, if there is no Syntax error just means the code correctly, does not mean that the correct logic. So we need to know for sure is the code we write what we want to do right by reading the code before compiling it first. This work is called  Desk Check  (Check the table). A program will need at Desk Checked thoroughly debug and contain fewer bugs than ever before.The reason is that all scenarios have been thoroughly predictable.

Sent a Test Plan

Test Plan  lists all of what we want to try and test them. When testing under Test Plan will discover the bug and try to type them out. Record the history of Test Plan (nothing wrong happens, you have to use any measures to solve) will useful in many ways. We will learn from experience and know Debug anything in the project have been tested in some way.

Error handling at run time

When an EXE written in VB6 program is running, if you encounter error, it displays an  Error Dialog to know why brief. After you click OK, the program will stop. If you run the program in VB6 IDE, you have the opportunity to stop the security program in place in the source code is error by pressing the button  Debugthe Error Dialog. Following that you can learn the value of variables to predict the cause of error. Therefore, if you start to use a program you write in the office, if convenient, in the first few weeks, instead of what running EXE program, you run the source code in the VB6 IDE. If a bug that happens, you can stop the program source code to debug.

When you use the statement:
    On Error Resume Next
then from there on, if it encountered error, it will ignore (ignore) completely. This pointmeans in place to help us avoid the program's EXE yoke fall and disappear, so that"home" to the customer. But it is also a disadvantage when customers said they expect the strange, unexplained (because of Error bỏ qua being that no one noticed), then wealso always know, can not know where to start to debug . Thus, while of course weshould not debug it, but before delivery to the customer you should consider carefullybefore use.

Use the Breakpoints

The best way to monitor execution of the program is to use  breakpoint  to make the program stop at a place we want in the code, then program step by step for us. In this occasion we will consider values of the variables to see if they are not as planned.

You anticipate where execution will go through the code, select a niche, then click on the left of the row code, where the red circle as the dot in the image below:


If you click the round red dots again, then it is canceled. Another way is to set a breakpoint on every code editor cursor and press  F9 . If you press F9 again when the cursor is located on the line, then cancel the break point.

At the program is stopped, you can see the value of a variable by letting her cursor over the variable, the tooltip will appear as shown below:


There are some other things you can do in this. You can take the red circle dot drag (drag) it is contrary to one or more of code to execute it will return some of the code. For each row execute your program code by pressing  F8 . Menu command is equivalent to  Debug | Step Into . There will be times when you do not want to enter the program in a Sub / Function that want to execute a Sub / Function as a simple step. In that case, you use the menu command  Debug | Step Over  or  Shift-F8 .


Remember to run your program press  F5 , the equivalent menu command  Run | Continue .
Sometimes you want to program stopped in the middle of a For Loop Iterator value when there is a fairly large value. If I was to pre a breakpoint in there and just press F5 repeatedly is a bit inconvenient. There is a trick to try using an IF statement when Iterator value is the value that we stop at the breakpoint at statement  Beep  (instead of what statement  ICounter Print ) like in the picture below:


Want to cancel all breakpoints you use the menu command Debug | Clear Allbreakpoints.
To ease the debugging, you can use the  Debug Toolbar  by displaying it with the Menu command  View | Toolbars | Debug


VB6 IDE will display the Debug toolbar as follows:

This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (593x236)

Use the Immediate Window

Immediate Window  allows you to execute the VB strong statement when program is stopped. We could use a print statement to show the value of a variable or the result of a function, called a sub or change a variable value before continuing to run the program again.

To display the Immediate Window, use the menu command View | Immediate Window.

Rather than hit  the "Print ICounter"  you can also type  "? ICounter" . Remember that each type of VB Statement will be thực Immediate Window when you press  Enter . You can reuse any VB statement in the Immediate Window, just press Enter at the end of it.

According footprint program (Tracing)

Sometimes no means to stop the program but you still want to know the program is doing in a Sub. You can leave the code of a Sub / Function a statement like this:

Debug.Print Format (Now, "hh: mm: ss") & "(Sub ProcessInput) Current Status:" &Status program to display the Status value in the Immediate Window when it executewithin the Sub ProcessInput what time.
Another way is instead displayed in the Immediate Window for you to write down ( Log ) into a text file.Here is a typical sub you can use for an Event Log message:

Sub LogEvent(ByVal GivenFileName, ByVal Msg As String, HasFolder As Boolean, IncludeTimeDate As Integer) 
   ' Append event message Msg to a text Logfile GivenFileName
   ' If GivenFileName is fullPathName then HasFolder is true
   ' IncludeTimeDate = 0 : No Time or Date
   '  = 1 : Prefix with Time
   '  = 2 : Prefix with Time and Date
   Dim FileNo, LogFileName, theFolder 
   If HasFolder Then 
      LogFileName = GivenFileName 
   Else 
      If Right(App.Path, 1) <> "\" Then 
         theFolder = App.Path & "\" 
      Else 
         theFolder = App.Path 
      End If 
      LogFileName = theFolder & GivenFileName 
   End If 
   FileNo = FreeFile 
   If Dir(LogFileName) <> "" Then 
      Open LogFileName For Append As FileNo 
   Else 
      Open LogFileName For Output As FileNo 
   End If 
   Select Case IncludeTimeDate 
   Case 0  ' No Time or Date
      Print #FileNo, Msg 
   Case 1  ' Time only
      Print #FileNo, Format(Now, "hh:nn:ss ") & Msg 
   Case 2  ' Date & Time
      Print #FileNo, Format(Now, "dd/mm/yyyy hh:nn:ss ") & Msg 
   End Select 
   Close FileNo 
End Sub 

Dùng Watch Window

Sometimes the program you want to stop at a certain place, but when the value of a variable or an expression of how much, maybe you do not know why a variable has a value natural way. Question:  Who is the culprit?  . For example when you want to stop program  ICounter = 15 . You can use the menu command  Debug | Add Watch . VB6 IDE will display the dialog below. You beat  ICounter = 15  in textbox  Expression  option box and click  the Break When Value Is True  box  Type Watch . Doing so means we want to stop the program by 15 ICounter.

Sterilization Methods Used Khai (Elimination Method)

There is a very common method is to debug code Comments Out of every doubt to see the bug has not disappeared. It's called  Elimination Method . If the bug disappeared, the code has been commented out of the culprit. You can Comment Out a row at the same time highlight the row was by then click  Block Comments  on the Edit Toolbar.
This image has been resized in order to avoid breaking the interface. Click here to view photo in full size (507x151)

When using the Elimination Method you have to consider the logic of your code when deciding Comment Out the goods, if not, it's a pretty dangerous approach.

In addition, Menu Command  View | Locals Window  to the numbers listed for all your variables in a Sub / Function and  View | Call Stack  hierarchical list of Sub turn called in from the outside to stop the current location code is time.
(Video Games)

0 comments:

Post a Comment