Avoid type casts - Use the "as" operator and check for null

Note: I don't care about measuring performance on these 2 operations because we don't use them in a tight loop.
For me its all about readability and robustness of my code.

Look at these code samples doing some type casts

        public List GetJobRulesFromPhysicalDB(DataTable renewedRules)
        {
            if (renewedRules.Rows.Count > 0 && CurrentJob.JobRules.Count > 0)
            {
                foreach (DataRow row in renewedRules.Rows)
                {
                    foreach (JobRule item in CurrentJob.JobRules)
                    {
                        if ((Guid)row["RuleID"] == item.RuleId)
                        {
                            item.UpdateRule(CurrentJob.RuleRepository.GetNewRuleByRuleId(item.RuleId));
                            break;
                        }
                    }
                }
            }

            return CurrentJob.JobRules;
        }
Figure: 1 code sample with untyped datatables

 

  
        private void AMControlMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var auc = (AMUserControl)sender; 
            var aucSessionId = auc.myUserControl.Tag;
            // snip snip snip
   
        }
Figure: Event handler in Silverlight

I see this VERY often, so I had to blog about it.

Silverlight - Prevent users from losing changes by using the browser back button

image

Do you know how to prevent users from losing changes in your Silverlight application, if they click the "back button" or "close browser" button?

quine in c#

A formatted version of the quine that I wrote in 2006

using System;
class MainApp
{
    public static void Main()
    {
        // quotation mark --> "
        char q = Convert.ToChar(34);

        // --> string b =
        string var = string.Concat(Convert.ToChar(115), Convert.ToChar(116), 
                Convert.ToChar(114), Convert.ToChar(105), Convert.ToChar(110), 
                Convert.ToChar(103), Convert.ToChar(32), Convert.ToChar(98), 
                Convert.ToChar(32), Convert.ToChar(61));

        // --> semicolon
        char s = ';';

        string a = "using System; class MainApp { public static void Main() { 
                char q = Convert.ToChar(34); string var = string.Concat(Convert.ToChar(115), 
                Convert.ToChar(116), Convert.ToChar(114), 
                Convert.ToChar(105), Convert.ToChar(110), 
                Convert.ToChar(103), Convert.ToChar(32), 
                Convert.ToChar(98), Convert.ToChar(32), 
                Convert.ToChar(61));   char s = ';'; string a = ";

        string b = "Console.WriteLine(a + q + a + q + s);Console.WriteLine( var + q + b + q + s + b); } }";


        Console.WriteLine(a + q + a + q + s); 
        Console.WriteLine(var + q + b + q + s + b);

    }
}

What is the difference between "StyleCop" vs. "VS2010 Code Analysis" vs. "FxCop"

I had this conversation the other day and started some investigation into this. Here is my quick recap from the below links

  • VS2010 Code analysis includes FxCop + more
  • VS2010 Code analysis and FxCop analyze assemblies
  • StyleCop is not part of the VS2010 Code analysis suite and checks C# coding style
  • StyleCop analyses source code

How to unit test a WCF service?

I am *very* keen on automating tests, so I was looking into unit testing a WCF service.

If I say "unit test", I mean a fast, in-memory, independent and automated test of a functional unit-of-work in the system.
Unit of work is at maximum one method (on my projects)
With thanks to Roy Osherove

Why should we unit test a WCF service?

Latest Posts

Popular Posts