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…


Comments (2)

  1. Karthikeyan says:

    So, you mean to say, that we should have a separate function and that should implement the logic instead of directly wrting into the event handler. so doing by this way we’ll have clarity in code. But we have a coding statandard where all the initialized objects sld be disposed at the end of the function, so we are forced to put try finally block in the function. but actally we sld have tr…catch block only in event handlers and not in the functions. so by placing try…finally block in function will hurt performance isn’t ?

  2. eferron says:

    Karthikeyan,

    You do not have to implement the catch block for the dispose pattern

    try

    {

    // your code…

    myInstance.SomeMethod(…);

    }

    finally

    {

    myInstance.Dispose();

    }

    Or you can implement the using pattern to ensure your object is disposed

    using(myInstance)

    {

    // do some work here no

    // need to call .Dispose(); with using

    }

    You want to protect system errors from getting to the User so the try/catch is essential for doing that in either the event itself or in the ThreadAbortException event. However waiting until the exception bubbles up to the ThreadAbortException event violates the principal of fail early fail often.

    The real expenses in try/catch code are throwing and rethrowing exceptions and the catch part of the block.

    HTH,

    Ed