03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Exercises ❘ 229<br />

Summary<br />

In practice, it’s extremely difficult to anticipate every condition that might occur within a large<br />

application. You should try to predict as many incorrect situations as possible, but you should also<br />

plan for unforeseen errors. You should write error-checking code that makes bugs obvious when<br />

they occur and recovers from them if possible. You may not anticipate every possible bug, but with<br />

a little thought you can make the program detect and report obviously incorrect values.<br />

You should also look for unplanned conditions (such as the user entering a phone number in a Social<br />

Security number field) and make the program react gracefully. Your program cannot control everything<br />

in its environment (such as the user’s actions, printer status, and network connectivity), but it<br />

should be prepared to work when things aren’t exactly the way they should be.<br />

You may never remove every last bug from a 100,000-line program, but you can make any remaining<br />

bugs relatively harmless and appear so rarely that the users can do their jobs in relative safety.<br />

Visual Studio provides a rich set of tools for debugging an application. Using the development environment,<br />

you can stop the program at different lines of code and examine variables, change variable<br />

values, look at the call stack, and call methods to exercise different pieces of the application. You can<br />

step through the program, executing the code one statement at a time to see what it is doing. You can<br />

even make some modifications to the source code and let the program continue running. Chapter 10,<br />

“Tracing and Debugging,” describes tools and techniques you can use to debug applications.<br />

Exercises<br />

1. Consider the following Student class.<br />

public class Student<br />

{<br />

public string Name;<br />

public List Courses = new List();<br />

}<br />

// Constructor.<br />

public Student(string name)<br />

{<br />

Name = name;<br />

}<br />

Add Debug.Assert statements to the class to require the following conditions.<br />

➤➤<br />

➤➤<br />

The Name property must always be at least 1 character long.<br />

The Courses value can never be null. (Although the list can be empty.)<br />

2. Repeat Exercise 1 using code contract preconditions and postconditions instead of Debug<br />

.Assert statements. (Put in postconditions even though you “know” they’re unnecessary.)<br />

3. Repeat Exercise 1 using code contract invariants instead of preconditions, postconditions,<br />

or Debug.Assert statements.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!