Professional Documents
Culture Documents
---
INF2011S (2016)
https://msdn.microsoft.com/en-us/library/sc65sadd.aspx
Objectives
Understand and fix Syntax & Logical errors. Adhering to Coding standards
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.
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
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
Check this
one!
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.
To examine the data, you can watch how their values change by opening up
some windows.
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.
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.
Examine the values in the calculate event procedure and fix the logic errors.
Shortcut Key
Start Debugging
Step 4: The Debugging Toolbar
-F5
Stop Debugging --
Shift + F5
Step Over
F10
Step Into
Control button
F11
Toggle BreakPoint
Step Out
F9
Shift + F11
Figure 2: MORE Debugging
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;
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)
For all Toolbox controls use camelCase and use the control name as a suffix.
See examples below:
o successButton o nameLabel
mainForm
phoneTextBox
Identify and fix possible situations like this in the given program
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:
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.
Are there any precautions that you should implement to avoid this WHY?
Break you program into login sections using the #region ...#endregion
statement.
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:
INF2011S
2016
Marksheet 1
Workshop (LAB) 1: DEBUGGING
STUDENTS NR________________NAME:_______________________
STEP
Description
Mark
Debugging Exercise
Step 5
5.1
5.2
5.3
Step 6
TOTAL MARK:_____________________/30
Tutor Name:_________________________________________________