Home > Products > Code Auditor > Screenshots > Tutorial

Fixing Sample Solution with Code Auditor
This tutorial will show you how to fix the sample "Regular Expression Editor"
project that came with Code Auditor.
We will show you how to run Code Auditor on the sample solution and step you through
each of the violated rules.
-
Getting started
Follow these steps to start auditing your sample solution:
- Download and install Code Auditor (See: http://www.ssw.com.au/ssw/CodeAuditor/).
- Open the sample solutions from start menu/SSW Code Auditor.
-

- Figure:
Starting sample solution from Start Menu/SSW Code Auditor
- Open NorthwindWindowsCS2005.sln in NorthwindWindowsCS2005 folder.
-

- Figure:
Open NorthwindWindowsCS2005.sln
- Click "Audit" on the toolbar in Visual Studio.
-

- Figure: Health Auditor toolbar (a Visual Studio add-in)
- Select source code to scan and click "Start".
-

- Figure:
Select project to scan
- Scanning...
-

- Figure:
Scanning in progress...
- Finished
-

- Figure: Click "OK" to see the result
- The report will now open.
-

- Figure:
Report in browser
- Close the report, go back to Visual Studio and see the error report in Output panel.
-

- Figure: The result in Visual Studio Output panel
Note: Make sure the Output panel is visible.
- Continue with tutorial to start fixing code! :)
Note: Double click on the error to navigate to error.
-
C# Code- Catch and re-throw exception improperly
Change from:
throw ex;
to:
throw; |
See rule
Do you catch and re-throw exceptions properly?.
-
C# Code- Catch Exception must be more specific
When an invalid regular expression is parsed in Regex.Match(), ArgumentException
will be thrown - and this is what we want to catch.
Change from:
catch (Exception ex)
to:
catch (ArgumentException ex) |
See rule Do you
catch and re-throw exceptions properly?.
-
C#/VB.NET Code- Application entry method should handle "UnhandledException" and
"ThreadException" events
Add the highlighted line:
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); |
And:
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show(e.ToString(), Application.ProductName + " " + new Version(Application.ProductVersion).ToString(2), MessageBoxButtons.OK, MessageBoxIcon.Error);
}
|
See rule Do you
use exception management application block?.
-
C#/VB.NET Code- Don't throw System.Exception
Change from:
throw new Exception("Not implemented
yet; This is a test.");
to:
throw new NotImplementedException("Not
implemented yet; This is a test."); |
See rule
Do you know that you should never throw an exception using System.Exception?.
-
C#/VB.NET Code- MessageBoxes must have icons
Change from:
MessageBox.Show("An error has occurred:" + Environment.NewLine
+ Environment.NewLine + ex.ToString(),
Application.ProductName + " " + new Version(Application.ProductVersion).ToString(2),
MessageBoxButtons.OK);
to:
MessageBox.Show("An error has occurred:" + Environment.NewLine
+ Environment.NewLine + ex.ToString(),
Application.ProductName + " " + new Version(Application.ProductVersion).ToString(2),
MessageBoxButtons.OK, MessageBoxIcon.Error); |
-

- Figure:
Bad - MessageBox without icon
-

- Figure:
Good - MessageBox with icon
See rule
Do you know how to make message boxes user friendly?.
-
C#/VB.NET Code- MessageBoxIcon.Question should not be used
Change from:
MessageBox.Show("File cannot be found.", Application.ProductName
+ " " + new Version(Application.ProductVersion).ToString(2), MessageBoxButtons.OK,
MessageBoxIcon.Question);
to:
MessageBox.Show("File cannot be found.", Application.ProductName
+ " " + new Version(Application.ProductVersion).ToString(2), MessageBoxButtons.OK,
MessageBoxIcon.Warning); |
-

- Figure: Bad - MessageBox with question icon
-

- Figure:
Good - MessageBox with warning icon
See rule
Do you know how to make message boxes user friendly?.
-
C#/VB.NET UI & Code- Buttons (except OK, Cancel, and Close), CheckBoxes, RadioButtons
must have mnemonics
Change from:
this.btnOpen.Text = "Open";
to:
this.btnOpen.Text = "&Open"; |
You can also do this using VS IDE designer:
-

- Figure:
Add the Mnemonic using VS IDE designer
-

- Figure: Bad - "Open" button does not have mnemonic
-

- Figure: Good - "Open" button has mnemonic
See rule Control
- Do your buttons have a mnemonic?.
-
C#/VB.NET UI & Code- OK, Cancel and Close buttons should not have mnemonics
Change from:
this.btnClose.Text = "&Close";
to:
this.btnClose.Text = "Close"; |
You can also do this using VS IDE designer
-

- Figure: Remove the Mnemonic using VS IDE designer
-

- Figure: Bad - "Close" button has mnemonic
-

- Figure: Good - "Close" button does not have mnemonic
See rule Control
- Do your buttons have a mnemonic?.
-
C#/VB.NET UI- FixedDialog must be used with CenterParent
Change from:
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
to:
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; |
See rule
Do you use inherited forms for consistent behaviour?.
-
C#/VB.NET UI- Winform should have its own icon instead of using the default .NET
application icon
Open the base form and add an icon:
-

- Figure:
Add an icon to the base form
-

- Figure:
Bad - Winform default icon
-

- Figure:
Good - Your own icon
See rule
Do you use inherited forms for consistent behaviour?.