Refactor Code

  • Gravatar
    Logging Exceptions!

    by Krammer on 5/8/2008 11:42:30 AM
  • I have the following flow for logging exceptions. How can I improve on this?
  • private bool AddCustomer(Customer customer)
    {
    bool result = false;

    try
    {
    // code to add the customer

    result = true;
    }
    catch (Exception ex)
    {
    // logg the exception
    Logger.Log(ex);
    }

    return result;
    }
  • Refactor it!
  • Gravatar
    Your code looks clean enough! But you can make is little better by introducing extension methods. This will make the code more readable.
    by azamsharp on 5/8/2008 12:48:31 PM
  • public static class ExtensionMethods
    {
    public static void Log(this Exception ex)
    {
    LogManager.Log(ex);
    }
    }

    private bool AddCustomer(Customer customer)
    {
    bool result = false;

    try
    {
    // code to add the customer

    result = true;
    }
    catch (Exception ex)
    {
    ex.Log(); // much nicer!
    }

    return result;
    }
Please log in to refactor the code! Login