This site works best in modern browsers. Looks like you're using an old one, you should upgrade if you can.

New Pluralsight Course - Implementing Feature Toggles in .NET with FeatureToggle

Feature toggles (also referred to as feature flags, flippers, feature switches, etc) are a technique to turn on or off features in applications. One way of categorising feature toggles is whether they are for the benefit of the development team (“release toggles”) or the business/stakeholders (“business toggles”). Release toggles are an alternative/compliment to feature branching and are short lived. Business toggles are long lived and represent application features that are enabled depending on business defined rules.

In my new Pluralsight course Implementing Feature Toggles in .NET with FeatureToggle I talk about the different types of toggles and some general things to consider when using toggles, followed by modules showing how to use the FeatureToggle open source library.

New Pluralsight Course - Introduction to .NET Testing with NUnit

If you are just dipping your toe in the water when it comes to testing .NET applications it can be a bit confusing. In addition to learning how and what to write test, you also have to learn a testing framework such as MSTest, xUnit.net, NUnit, etc.

My new beginner Pluralsight course helps you to get started with testing in .NET and how to use the NUnit testing framework.

Watch the course at the above link or get to it from my Pluralsight author page.

New Pluralsight Course - Automated ASP.NET MVC Testing: End to End

My new Pluralsight course Automated ASP.NET MVC Testing: End to End was just published.

The course looks at tools and techniques to be able to tests models, controller actions, Razor view rendering logic, and implement automated functional UI browser testing.

The course wraps up by showing how to design and implement a continuous integration build in TeamCity that runs the tests created during the course.

Check it out the full table of contents.

Using Cyclomatic Complexity as an Indicator of Clean Code

Cyclomatic complexity is one way to measure how complicated code is, it measures how complicated the structure of the code is and by extension how likely it may be to attract bugs or additional cost in maintenance/readability.

The calculated value for the cyclomatic complexity indicates how many different paths through the code there are. This means that lower numbers are better than higher numbers.

Clean code is likely to have lower cyclomatic complexity that dirty code. High cyclomatic complexity increases the risk of the presence of defects in the code due to increased difficulty in its testability, readability, and maintainability.

Calculating Cyclomatic Complexity in Visual Studio

To calculate the cyclomatic complexity, go to the the Analyze menu and choose Calculate Code Metrics for Solution (or for a specific project within the solution).

This will open the Code Metrics Results window as seen in the following screenshot.

image

Take the following code (in a project called ClassLibrary1):

namespace ClassLibrary1
{
    public class Class1
    {
    }
}

If we expand the results in the Code Metrics Window we can drill down into classes and right down to individual methods as in the following screenshot.

image

Read full article...

The ConditionalWeakTable in .NET

The ConditionalWeakTable class exists in the System.Runtime.CompilerServices namespace and as its namespace suggests is used in compilation processes.

The class allows a key-value pair to be stored (using its Add method), once the key object no longer has any references outside of the ConditionalWeakTable the key can be destroyed during garbage collection as the key is held as a weak reference inside the table, as opposed to a usual strong reference. At this point the key object can be garbage collected. Also at this point, if the value object also has no other references, it too can be collected. Or too quote MSDN: “It does not persist keys. That is, a key is not kept alive only because it is a member of the collection”.

The following code shows a console application followed by its output:

using System;
using System.Runtime.CompilerServices;

namespace ConditionalWeakTableExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var cwt = new ConditionalWeakTable<SomeKeyClass, SomeValueClass>();

            Console.WriteLine("Creating new instance of SomeKeyClass called key1");
            var key1 = new SomeKeyClass();

            Console.WriteLine("Creating new instance of SomeValueClass called value1");
            var value1 = new SomeValueClass();

            Console.WriteLine("Creating variable anotherReferenceToKey1 referencing key1");
            var anotherReferenceToKey1 = key1;

            Console.WriteLine("Adding key1, value1");
            cwt.Add(key1, value1);




            Console.WriteLine("Press a key to set key1 to null");
            Console.ReadLine();
            key1 = null;
            Console.WriteLine("key1 to null");

            ForceGc();




            Console.WriteLine("Press a key to set anotherReferenceToKey1 to null");
            Console.ReadLine();
            anotherReferenceToKey1 = null;
            Console.WriteLine("anotherReferenceToKey1 to null");

            ForceGc();            




            Console.WriteLine("End of program - press any key to exit");
            Console.ReadLine();
        }


        private static void ForceGc()
        {
            Console.WriteLine("Forcing garbage collection and waiting for finalizers");
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }


    class SomeKeyClass
    {
        ~SomeKeyClass()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("****** SomeKeyClass Destroyed");
            Console.ResetColor();
        }        
    }

    class SomeValueClass
    {
        ~SomeValueClass()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("****** SomeValueClass Destroyed");
            Console.ResetColor();
        }
    }
}

image

 

So here, even though the cwt contains an entry using key1, it is held in a weak way so it will not prevent garbage collection.

Also notice in this example, the value object has not been destroyed as we still have a reference to it in the variable value1.

If we change the code to also set value1 to null, when the Garbage Collector runs it will destroy the key object as before, but now there are also no references to the value object, so it too can be destroyed:

static void Main(string[] args)
{
    var cwt = new ConditionalWeakTable<SomeKeyClass, SomeValueClass>();

    Console.WriteLine("Creating new instance of SomeKeyClass called key1");
    var key1 = new SomeKeyClass();

    Console.WriteLine("Creating new instance of SomeValueClass called value1");
    var value1 = new SomeValueClass();

    Console.WriteLine("Creating variable anotherReferenceToKey1 referencing key1");
    var anotherReferenceToKey1 = key1;

    Console.WriteLine("Adding key1, value1");
    cwt.Add(key1, value1);




    Console.WriteLine("Press a key to set key1 to null");
    Console.ReadLine();
    key1 = null;
    Console.WriteLine("key1 to null");

    ForceGc();




    Console.WriteLine("Press a key to set anotherReferenceToKey1, value1 to null");
    Console.ReadLine();
    anotherReferenceToKey1 = null;
    value1 = null;
    Console.WriteLine("anotherReferenceToKey1 & value1 to null");

    ForceGc();            




    Console.WriteLine("End of program - press any key to exit");
    Console.ReadLine();
}

 

image

 

So even though this is not a class we’d use in everyday coding, it’s one of the many interesting things in .NET that are hidden away in namespaces we don’t usually use. It also should be noted that it doesn’t really behave as a “normal” dictionary, for example it doesn’t contain GetEnumerator method.

To learn some cool C# Tips, check out my free C# Tips eBook or my Pluralsight C# Tips and Traps course.

about jason

My Bio Photo

Jason Roberts is a Journeyman Software Developer, Microsoft MVP, writer, Pluralsight author, open source contributor and Windows Phone & Windows 8 app author.

He holds a Bachelor of Science in computing and is an amateur music producer and landscape photographer.

MVP Logo

Pluralsight Author Badge

Sign up for the Don't Code Tired Newsletter

Disclaimer
The opinions expressed herein represent my personal opinions and do not represent my employer's views in any way.

© Copyright 2015, Jason Roberts