Tuesday, September 07, 2010

Don't be too lazy and avoid the type "Tuple"

The Tuple type is great!!!
That's what I was thinking when I heard the 1st time about it.
It's a nice and easy way to group elements of a different type together. Sweet!

I started first using the Tuple type as a return value from some methods where I had an out parameter before

E.g. I refactored this

            string errormsg;
            User user;
            bool success = AuthenticationService.GetUser(usercode, out user, out errormsg);
Figure: We should avoid out parameters, because it means we return 2 things from a method. More on Stackoverflow here and here

Thursday, September 02, 2010

SQL Server - Generate triggers for your "LastModified" columns with a fancy SQL script!

Every table in our database should have a "LastModified" column to record the last modified time of each row.
image[5]
Figure
: Sample table with a LastModified column

Our rule "Do you have standard Tables and Columns?" says the column should be called "DateModified" which has the same purpose.

How do you populate that field?

Thursday, August 26, 2010

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.

Monday, August 23, 2010

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?

Tuesday, August 17, 2010

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);

    }
}

Wednesday, August 11, 2010

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

Friday, August 06, 2010

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?

Saturday, July 31, 2010

Inconsistent code will bite you sooner or later - I reckon sooner ;-)

See this sample interface with some method signatures

void CalculateSalary(string environment, string emloyeeName, ...)

void GetListOfSubEmployees(string environment, string emloyeeName, ...)

void CalculateBonusPerYear(string environment, string emloyeeName, ...)

void GetOrderRegion(string environment, string emloyeeName, ...)

void UpdateEmployee(string environment, string emloyeeName, ...)

void MergePolicy(string emloyeeName, string environment, ...)

As you can see all methods have some common parameters (string environment, string emloyeeName).

Monday, July 19, 2010

"Hidden" shortcuts for testers in Visual Studio

Like most of us, I learned the following shortcuts from the menu
image
Figure: VS Run tests menu items with shortcuts

Ctrl+R,T Run tests in current context
Ctrl+R,A Run all tests in solution
Ctrl+R,Y Run all impacted tests

with their corresponding Debug Mode, by using <Ctrl> for the second keystroke. E.g. Ctrl+R,Ctrl+T Debug tests in current context

Today I discovered these additional shortcuts.
I discovered those by changing some shortcuts from using MSTest to TestDriven, as per suggestion from RBanks54 (which rocks BTW, both)

Tuesday, June 29, 2010

Avoid boolean parameters in method parameters

*Updated* 12/07/2010: Response from Uncle Bob added at the end


Uncle Bob has a coding rule: Avoid boolean parameters in method parameters

He preaches that up and down the street, and I couldn't agree more with that!

The reason:

  • We all know that 1 method should do only 1 thing (Single Responsibility Principle  for methods and classes)
  • If your method has a boolean parameter it is highly possible that your method is doing 2 things