5 Handy Hints for Writing .NET Programs Posted by: Xav in Untagged  on
Well well well. Anyone can write a program. Well, maybe not everyone. But it isn't that difficult. However, not everyone can write a good program. There are a few pieces of advice when writing programs, that any decent programmer should bear in mind. Here's a brainstorm:

Note:  code references are to .NET, but techniques can be applied across languages.

1. Keep it responsive
A program can do something amazing, but it's no good if it just freezes up. A user who has to wait for a program to unfreeze is not a happy user, and bear in mind that different computers have different levels of processing power, so it is important to allow for this.

A good way to ensure responsiveness is to use Application.DoEvents() in .NET, like so:

 
  1. Application.DoEvents();


 
  1. Application.DoEvents()

You can also use multithreading, but in either case, you also need to:

2. Tell the user what's going on
So the user clicks on "Go". The application might not freeze (like above), but the user needs to know if the program is doing something - otherwise, they might not know an action is being performed.

A StatusBar is very useful - the bar that sits at the bottom of the screen. In its simplest form, a simple label that sits in the bottom-left corner of the screen, that tells the user what's happening. You could create a method like this:


 
  1. private void Status(string Msg)
  2. {
  3.  statusLabel.Text = Msg;
  4. }


Then, to use the code, just simply call the method, passing it a string parameter:

 
  1. Status("Getting data...");

Another useful option is to use a ProgressBar. This is the bar that can be filled to show the ongoing progress of an action. To use it, add one from the Toolbox, then set the Maximum and Minimum values in the Properties Window. To set the value in code, simply change the property:

 
  1. progressBar1.Value = 56;

Assuming the Maximum property is set to 100, the progress bar will be set to just over half full.

3. Stick with what they know
This is an easy one - don't confuse the user. Make use of well-known programs, such as Office. For example, always use Ctrl-S for saving, Ctrl-P for printing and Ctrl-F for finding. This way, a user does not need to learn a new set of shortcuts. On menus, you can easily set this using the Properties Window.

Also, copy other programs' layouts. The more you familiarise yourself with the 'common' program layouts, the more familiar your programs will seem to users. Learn that the menu goes at the top, the status bar goes at the bottom. Buttons are always aligned, text boxes are generally the same length. Important actions such as deleting and closing should always be confirmed with a simple dialog, perhaps in the form's FormClosing event. To cancel the closing of a form, use:

 
  1. e.Cancel = true;

The event argument 'e' contains data about the FormClosing event. The Cancel property is a boolean value - simply set it to true to stop the form from closing. A good idea is to use MessageBox.Show(), passing in the four standard parameters - the text, the title, and two enumerations describing which buttons and icon to display.

4. Test it, test it, test it again
Bugs, bugs, bugs, Every program is vulnerable to bugs, and they need to be found and eliminated ASAP. Run the program, making things extreme. Try your best to cause an error (known as an Exception), then swoop in and work out what went wrong. Then, fix the error. If you need to trap errors when the worst happens, use a try/catch block, like so:

 
  1. try
  2. {
  3. //Code goes here.
  4. }
  5. catch (Exception exc)
  6. {
  7. //If exception occurs in try section, code execution redirects here. The 'exc' variable contains data about the exception.
  8. }
  9. finally
  10. {
  11. //This code ALWAYS runs, regardless of whether or not an error occurs.
  12. }

If possible, get friends and family to test the program for you, or if you're a pro, hire a software tester. Errors don't go down well... but if they do, then they need to be able to contact you for...

5. Help!
Whether your program is complex or not, users are bound to need to contact you in some way. Always include a Help menu on your menu strip at the top, preferably with a link to a website/email. You can make it load the page automatically, using a WebBrowser control. Users can request assistance, alert you of bugs and even donate - so you get paid for all your hard work. :)

Trackback(0)
feed3 Comments
Jordan
July 01, 2008
Votes: +0

DoEvents() is a good tip that I see most new programmers over looking. A frozen application looks horrible. For #4 it also helps to run your application on as many different versions of Windows as possible (XP SP1, SP2, Vista, 2000, etc). I use VmWare and Virtual PC for this.

I'd like to add a rule though: ALWAYS ASSUME THE USER IS STUPID.

Not to be mean or anything but this will save a lot of Q&A later down the road. Excellent blog! Looking forward to your next one. smilies/grin.gif

report abuse
vote down
vote up
WingedPanther
July 02, 2008
Votes: +0

These are good tips for any language. I use the delphi equivalent of DoEvents() even in my DB migrations, just so I can move the window around, etc.

report abuse
vote down
vote up
Xav
July 05, 2008
Votes: +0

Yeah - although it isn't perfect, and slows calculations down considerably when used in a loop.

report abuse
vote down
vote up

Write comment
 
 
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger
 

security image
Write the displayed characters


busy