Sei sulla pagina 1di 21

Debug Example

---

INF2011S (2016)

HOMEWORK WORKSHOP 1 for Week 2 - Debugging

https://msdn.microsoft.com/en-us/library/sc65sadd.aspx

Objectives

The main objectives of this exercise is to

Understand and fix Syntax & Logical errors. Adhering to Coding standards

Understand debugging tools

Be able to examine data using expressions in the debugging windows Be able


to debug objects and procedures.

Understand what is meant by maintainability and extensibility of programs

Debugger Roadmap: http://msdn.microsoft.com/en-us/library/k0k771bt.aspx

Debugging YouTube video: https://www.youtube.com/watch?v=FHnxx99DudI

Step 1: Syntax & Logical errors

Syntax Errors

Normally the compiler will report on a syntax error. A syntax error happens
when program statements violate the grammar rules of a language. A syntax
error is due to a misuse of the C# language in your code. For example, C# is
case-sensitive and, has a set of keywords that you should (must) not use to
name your variable. The first rule of case-sensitivity can be difficult to
observe if you come from a case-insensitive language like Pascal or Visual
Basic. It can also happen through distraction. The second rule is easy to
observe if you have used another C-based language such as C++ or Java.
Both rules are easy to violate if you write your code using a normal text
editor like Notepad in VB.NET an error message appears in the Task window
and the statement that caused the error is normally underlined.

Find the program DebugEx_LAB01 from Vula under Practicals

Identify and fix any syntax errors in the program.

Logical Errors

Logical errors are more difficult to find and correct. Logic errors can be
thought of as coding errors. They occur when the program (the code) is
written fine but the result it produces is not reliable. With a logic error, the
Code Editor does not see anything wrong in the document and therefore
cannot point out a problem. One of the worse types of logic errors is one that
makes a computer crash sometimes, regularly, or unpredictably, while there
is nothing obviously wrong in the code. They can be quite tricky to track
down, and have you tearing your hair out with frustration. You will often hear
yourself saying "But that should work! Why won't it!"

Homework_Workshop1_Debug2016.docx 1

A logic error is called a bug. Debugging is the process of examining


code to look for bugs or to identify problems. It is the process of finding
logical errors and correcting them. Debugging is the ability to monitor the
behavior of a variable, a class, or its members throughout a program.
Microsoft Visual C# provides many features to perform debugging
operations. Debuggers provide a set of tools (see Figure 1) to allow a person to
analyse a program while running. This enables the user to suspend the execution
and investigate the values of different variables.

Step 2: Breakpoints

One of the most used tools that enable a programmer to debug a program is
a Breakpoint. A breakpoint on a line is the code where you want the
execution to suspend. You must explicitly specify that line by creating a
breakpoint. You can as well create as many breakpoints as you want. You can
also remove a breakpoint you don't need anymore

You would insert a breakpoint in your code to suspend the execution of the
program.

From this point you will then be able to control the executing by either
executing one line at a time or a full procedure at a time.

This will allow you to inspect the values of variables at each step.

We call this process debugging and it will enable you to find logic errors much
easier or to understand the logic of your program better.

Remember that you can create more than one breakpoint. If you have more
than one breakpoint in your code, execution would pause at each one of
them. At any time, you can remove one or all breakpoints. To delete all

breakpoints, on the main menu, click Debug -> Delete all Breakpoints.
debug
In addition to the syntax
error(s), the program
DebugEx_LAB01, contains
logic errors and you will have to

the program to find the errors.

If the program works correctly


it is supposed to find the
factorial of the number 10. The
factorial of the number 10 (10!)
is calculated as follows: 10 * 9 *
8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 and
the answer should be 3628800.

To enable the debugger,


compile the program using the
debug configuration, see Figure
1.

Check this
one!

Figure 1: Debugger Options

Inserting BREAKPOINTS --

You can insert a break point next to a line where you want the execution to be
suspended to investigate the values of the variables.

To insert a breakpoint (brick(red) Dot ) click in the grey column to the left of
the line where you want the break point to appear (or right click in the grey
column next to the line and choose Insert Breakpoint). Breakpoints can be
inserted anywhere next to any line, except next to declaration statements on
form level.

Start the debugging process Debug > Start on the menu bar and click the
calculate button on the form. (Start Debugging (F5))

Notice that the heading (title bar) in the IDE displays [break] to indicate break
mode.

A yellow arrow will appear on the breakpoint (red Dot), the line will be
highlighted in yellow and execution will stop.

If you hover with the mouse pointer over the variables you can see the value
of that variable.

Click on the red dot again to remove the breakpoint Experiment with break next
to different lines.

Step 3: Examining Data

To examine the data, you can watch how their values change by opening up
some windows.

These windows can ONLY be shown in Break mode (debug mode).

Set the breakpoint and then start the project in debug mode.

Debug > Start, execution will be suspended when the line with the breakpoint
is reached. From the menus choose Debug > Windows > Watch.

Open a particular watch window (watch 1, 2, 3, or 4) and type the name of


the variable you would like to watch in the window.

You can also add a Watch window to watch all the local variables in a
particular block of code; Debug > Windows > Local, this will list all the
variables local to the block of code you are in for you to watch their values. (If
you do not see the Watch right click when on x and choose Add
watch).Expressions can also be entered in the Watch window Like x * i the
value for this will then be shown.

Delete all unwanted expressions in the watch window.

Examine the values in the calculate event procedure and fix the logic errors.

Shortcut Key
Start Debugging
Step 4: The Debugging Toolbar

-F5

The Debug toolbar (see Figure 1 for the


symbols and shortcut keys) contains
button to control the debugging
process. Instead of toolbar, shortcut
keys can be used to activate the same
functionality. Use the debugging tools
in the opposite window to fix the logic
errors of the given program.

Stop Debugging --

Shift + F5
Step Over

F10
Step Into
Control button

F11

Toggle BreakPoint

Step Out
F9
Shift + F11
Figure 2: MORE Debugging

STEP 5: Coding Standards

5.1 Naming standards

Ensure that the code in the program adheres to good naming standard and
change variable names in those cases where this was not the case.

There are several different forms of casing that will be used throughout the
conventions: Pascal Case: Initial caps at the start of each word. Example:
PascalCase (class names;

method names; Windows file names)

Camel Case: Initial caps at the start of each word except the first. Example:
camelCase (all names for Windows controls)

Lower Case: All lower case. Example: lowercase (all web file names)

DO NOT USE --Hungarian: Initial type tag in lower case (with possible
modifiers), followed by optional Pascal Case basename. Example:
strHungarian (all basic variables like string,integer, decimal etc.)

Always use standard names for objects use singular for one object and
plural for collections (Camel Case e.g. aDisk)

No spaces or punctuation marks

For all Toolbox controls use camelCase and use the control name as a suffix.
See examples below:

o successButton o nameLabel

mainForm

phoneTextBox

5.2 Programming Principles 1 -- Variable Declaration

It is necessary to declare as few variables as possible on form level. Only


those used in most methods of a class should be considered for declaration
on form level. Variables belonging to one method only should be declared
locally in a procedure.

Identify and fix possible situations like this in the given program

ADD a Comment at the bottom of the code in DebugEx_LAB01,to


state what you have

5.3 Programming Principles 2

Each method should in all cases perform a single task. For example the
calculateButton_Click event is supposed to act on the interface. It therefore
should not contain the steps of how to perform the calculation. For this one,
rather use another method. You need to decide:

whether this method returns a value or not

what values should be passed to this procedure to enable it to calculate the


factorial of a number

STEP 6: Maintainability - Extensibility

Maintainability is the ability of the system to undergo changes with a degree of


ease. These changes could impact components, services, features, and
interfaces when adding or changing the functionality, fixing errors, and meeting
new business requirements. Maintainability can also affect the time it takes to
restore the system to its operational status following a failure or removal from
operation for an upgrade. Improving system maintainability can increase
availability and reduce the effects of run-time defects. An applications
maintainability is often a function of its overall quality attributes. Go to this
website to read on a number of key issues that can directly affect
maintainability: http://msdn.microsoft.com/enus/library/ee658094.aspx#Maintainability

Using the program DebugEx_LAB01:

Will the program provided calculate the factorial for any given number
successfully? How easy is it to change this if not?

What changes do you have to make that your programming can run for any
valid value entered.

What happens if the number becomes too large?

Are there any precautions that you should implement to avoid this WHY?

Break you program into login sections using the #region ...#endregion
statement.

For the Champs!


Change the program to be more effective.

Having the factorial calculation IN the Form is NOT good programming. A form
should only for part of aan interface that gets input and displays output.

Change the program and use a Factorial class (that you need to design) to
manage the the attributes and behaviours of a factorial object.

Extra Material:
You are requested to do readings on the following:

General Naming Conventions .NET Framework 4.5: http://msdn.microsoft.com/enus/library/ms229045.aspx

Whats New for the Debugger in Visual Studio 2015 http://msdn.microsoft.com/enus/library/vstudio/01xdt7cs.aspx

INF2011S
2016

Marksheet 1
Workshop (LAB) 1: DEBUGGING

STUDENTS NR________________NAME:_______________________

STEP

Description
Mark

Debugging Exercise

List the syntax errors in the program

At what line did you add the breakpoint?

Which variables did you watch?

List the logic errors in the program

In the open blocks below, write down descriptions and

resolutions for all the issues addressed in Steps 4 and 5.

Please place these answers in your program as

comments at the bottom of the form.

Step 5
5.1

5.2

5.3

Step 6

TOTAL MARK:_____________________/30

Tutor Name:_________________________________________________

Potrebbero piacerti anche