Welcome to AspAdvice Sign in | Join | Help

Skills versus Passion, are they the same?

I would like to take the time to address a problem that we all probably have encountered and maybe been guilty of ourselves from time to time. This subject is performance.

During my time as working as a software developer I have been honored to have worked side-by-side with knowledgeable and passionate people. The flip side of the coin is I have also worked side-by-side with people who have the skill set but lack passion to produce quality code and to revisit problematic areas. I have heard many times over, “what does it matter, it works!”. Well draining the oil from your vehicle then driving it down the road will work also but sooner than later you will lock down your engine. With that stated I am not saying I am perfect and no one is, but there are times that we must and should swallow our pride and take a hard look at poorly wrote code. Here is an example.

try
{
 myValue = Convert.ToString(Context.Session["objectId"]);
}
catch
{
 myValue = "0";
}
Example: 1

How many problems do you see in example 1?

In my opinion while a try/catch statement has its place, example one is not one of these cases. Rather in my opinion this statement would better be served as indicated in example 2.

if (!String.IsNullOrEmpty(Context.Session["objectId"].ToString()))
 _success = int.TryParse(Context.Session["objectId"].ToString(), out myValue);
else
 myValue = 0;
Example: 2

Interestingly enough most seasoned developers understand the performance cost of exception handling and at times it can be very expensive. Case in point; take a look at the following console application. (Note: I previously found a blog entry the presented this demonstration but for the life of me I can not located the URL.)

using System;
using System.Collections.Generic;

public class MyClass
{
 public static void Main()
 {
  ExceptionPerformance();
 }

 private static void ExceptionPerformance()
 { 
  const int _iterations = 50000;
  Console.WriteLine("Standby... Running " + _iterations + " iterations. This may take 30 to 60 seconds!");

  DateTime start = DateTime.UtcNow;
        for (int i=0; i < _iterations; i++)
        {
            try
            {
                throw new ApplicationException();
            }
            catch (ApplicationException)
            {
            }
        }
        DateTime end = DateTime.UtcNow;
        long _millis = (long) (end-start).TotalMilliseconds;
       
        Console.WriteLine("Total time taken: {0}", end-start);
        Console.WriteLine("Exceptions per millisecond: {0}", _iterations/_millis);

 }
}
Example: Console Application

As I executed this console application take note of the results and decide what you think.

Standby... Running 50000 iterations. This may take 30 to 60 seconds!
Total time taken: 00:00:02.2984959
Exceptions per millisecond: 21

Assuming my math skills are still sharp which is saying a lot, my conclusion is it took 2.3 seconds to execute at a total of 48,300 exceptions raised.

Also another mistake that I see all to often is swallowing an exception. Take a look at the following.

try
{
 ddlControl.DataSource = object.Tables[0].DefaultView;
 ddlControl.DataTextField = "myValue";
 ddlControl.DataValueField = "myId";
 ddlControl.DataBind ();
}
catch{}
Example: 3

There have been many times over the years that I have seen these types of issues and at times I simply want to throw my hands up in the air. As I previously stated, I make mistakes as does everyone but the key is to learn from your mistakes and not make them in the future. I admit that I still have a great deal to learn and I firmly believe that my passion drives me to become a better developer. There are many people within the .NET community who I would be honored to be able to work with one day and in my opinion a junior or mid level developer with a strong passion for bettering themselves could become a top level developer with the correct mentors.

In conclusion, I would be very interested in your thoughts on this matter both on the points of code performance and from the perspective of mentoring junior or mid level developers.

References:

Exceptions and Performance
Benchmarking IsNumeric Options

I have spoken on this topic with a variety of people with the .NET community and I wanted to thank you all for your viewpoints.

Published Thursday, May 10, 2007 2:01 PM by sswafford

Comments

New Comments to this post are disabled