User Interface Programming Tip #1 – Writing maintainable code and Code Complexity

When adding code to event handlers to a form code behind (i.e. your button click event), consider the following guideline(s).    

 

Place as little code in the event handler as possible.  You should delegate the actual functionality out to a user interface process block or business object or some other type of controller depending on what pattern you are implemeting.  The code in the event itself should only dispatch to some internal implementation handler and make sure system generated errors do not get to the end user.

 

Consider the following example

 

[MySampleForm.cs]

public class SampleCode

{

  private void NewButton_Click(object sender, EventArgs args)

  {

  try

  {

      this.MousePointer = Cursors.AppStarting;

   CreateNewItem(…);

  }

  catch(PermissionException permissionException)

  {

    // Safe to display this message to the user and provide options

    // or suggestions on how to proceed

   PermissionExceptionHandler();

  }

  catch(System.Exception systemException)

  {

  // something unexpected happened, log the exception

    Program.FatalExceptionHandler

  }

    finally

    {

      this.MousePointer = Cursors.Default;

    }

  }

}

[Program.cs]

public class Program

{

  [STAThread()]

  public static void Main(string[] args)

  {

    Application.Run(new MySampleForm());

  }

  public static void FatalExceptionHandler(...)

  {

    // 1) Notify the user with safe generic message

    MessageBox.Show(…);

    // 2) If you can save the state of the application so the user

    // can pick where they left off when exception happened

    Program.SaveApplicationState();

    // 3) Let the user know the application needs to be restarted

    Program.RestartApplication();

  }

}

 

In my example here, one could argue the cursor handling code should be moved to the CreateNewItem(…) routine, I think you get the still get the point.

 

I know you may have heard this one before, even so I have participated in several code reviews with several lines of code dealing with application logic directly in the event handler.  The entire point to this post is to have all of my friends in C# land to keep the code behind those events in your UI short and simple.

 

Use Code Complexity Tools

 

Create code that is easy to maintain looking for areas to reduce code complexity.  There are tools to help do this check out a few listed below:

 

 

In addition review Mike Swansons blog on code complexity here.

 

Happy coding…