StyleCop 4.3

A new version of StyleCop has been released, and they are about to release their SDK documentation.

The new rules shipping with StyleCop 4.3 include:

· Enforce sort order for using directives. This aligns with the Organize Usings functionality in VS 2008

· Require standard summary text for constructors and destructors

· Require parenthesis in arithmetic and comparison expressions for added clarity

· Require String.Empty rather than “”

· Require explanation message text in debug asserts

· Require justification text in Code Analysis suppressions

· Enforce use of built-in type aliases for int, string, float, etc.

· Require a blank line between elements

· Disallow blank lines at the top or bottom of documentation headers

· Disallow regions within method bodies (enabled by default)

· Disallow the use of regions anywhere within the code (disabled by default)

· Disallow empty static constructors

· Disallow empty unsafe, lock, checked, and unchecked statements

· Disallow unnecessary or empty try\catch\finally

The 4.3 release is available here: https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=sourceanalysis&ReleaseId=1425

Posted by Pieter | with no comments

2TB, 64 Core System

I want one of these!

image

Stole the image from Mark Russinovich’s blog.

Posted by Pieter | with no comments

New ActionThis Release

The ActionThis guys have been busy this weekend with a new release:

The following changes were released last Sunday.

Improvements to the Project area:

  • The Project and Action Item pages have been merged.  The new Project page has an Action Item and a Details tab.  The Project page functionality can be found on the Details Tab, while the Action Item page functionality can be found on the Action Item tab.
  • A team member can leave a project they are signed to by going to the Details tab and clicking “Remove me.”
  • We have created the “(Other)” project that will display all Action Items assigned to you from Projects you are not a member of.
  • A new tick box to allow the viewing “Show Closed Action Items” instead of the two “View Open” and “View Closed” text.

Parent/Sub Action Items on Action Profile Page:

  • A Sub Action Items can now be created in the Action Profile Page by going to the Sub Action Item section and clicking “Add Action Item."
  • Rollover command functionality (from Project page) is also available.
  • When viewing a Parent Action Item you can navigate to a Sub Action Item by selecting it in the Sub-Action Item section.
  • When viewing a Sub Action Item the Parent Action Item is displayed below the “Status:”
  • When viewing a Sub Action Item you can navigate to a Parent Action Item by selecting its display name.

Outlook Client:

  • When working offline the changes to an action item's workflow are now reflected correctly in the Workflow buttons (e.g. Accepting an Action Item off-line now causes the “Accept” button to be greyed out when the item is next opened and the action is recorded in the item's history).
  • A Sub Action Item can now be created by right-clicking on the Parent Action Item and selecting “New Sub Action Item."
  • Action items can now be dragged to different projects or even be moved to be a Sub Action Item of another Action Item.

If you have any questions or feedback please email ATSupport@ActionThis.com.

Thanks for your continued support.

The ActionThis Team

Posted by Pieter | with no comments
Filed under:

Equality Pattern from Resharper

I was just reading this blog entry on how Resharper can auto generate equality members.

This is a pretty nice pattern to follow even when doing this by hand.

Here is the class it was created for:

public class Fooberry
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

 

public override bool Equals(object obj) {   
    if (ReferenceEquals(null, obj)) return false;   
    if (ReferenceEquals(this, obj)) return true;   
    if (obj.GetType() != typeof (Fooberry)) return false;   
    return Equals((Fooberry) obj);   
}   
  
public bool Equals(Fooberry obj)   
{   
    if (ReferenceEquals(null, obj)) return false;   
    if (ReferenceEquals(this, obj)) return true;   
    return Equals(obj.Foo, Foo) && Equals(obj.Bar, Bar);   
}   
  
public override int GetHashCode()   
{   
    unchecked  
    {   
        return ((Foo != null ? Foo.GetHashCode() : 0)*397) ^ (Bar != null ? Bar.GetHashCode() : 0);   
    }   
}  

 

I’ve know about most of this pattern including ReferenceEquals, but look at the unchecked keyword.  Very cool indeed.  I do wonder what the significance of the ‘* 397’ is though.  Any ideas?

Posted by Pieter | with no comments
Filed under:

Using Linq to Filter a list

So yesterday I made this post on applying a filter or map on a list.  Now let’s have a look at how we can do this with Linq.

First the list:

List<string> list = new List<string>();
list.Add("Anne");
list.Add("Brian");
list.Add("Pieter");
list.Add("Wayne");
list.Add("Shane");
list.Add("Susan");
list.Add("Xavier");

Plain old Linq:

// Plain old Linq
var linqFiltered = from item in list
                            where item.StartsWith("S")
                            select item;

Using the Where extension method:

// Linq with the Whete extension method only
var linqListFiltered2 = Enumerable.Where(list, i => i.StartsWith("S"));

And for me the most readable:

// Linq using the Where extension method on the list
var linqListWhereFiltered = list.Where(i => i.StartsWith("S"));

You can even create your own Where method:

static IEnumerable<T> Where<T>(IEnumerable<T> sequence, Func<T, bool> predicate)
{
    foreach (var item in sequence)
        if (predicate(item))
            yield return item;
}

And you can use it like this:

var myLinqListFiltered = Where(list, i => i.StartsWith("S"));

I see so many uses and possibilities for the Linq style code as well as extension methods.

In fact I can easily change the last example to be a bit more clear:

static IEnumerable<T> Filter<T>(this IEnumerable<T> list, Func<T, bool> predicate)
{
    foreach (T item in list)
        if (predicate(item))
            yield return item;
}

and then the intent is much clearer:

var myFilteredList = list.Filter(item => item.StartsWith("S"));
Posted by Pieter | with no comments
Filed under: ,

Filter and Map in C#

I was reading this post by Sarah Taraporewalla.  And I immediately wanted to see if I could do this with C#.

Here are my results for Filter:

using System;
using System.Collections.Generic;
 
namespace TestFilter
{
    static class Program
    {
        
        static void Main(string[] args)
        {
            // Add a whole bunch of names
            FilteredList list = new FilteredList();
            list.Add("Anne");
            list.Add("Brian");
            list.Add("Pieter");
            list.Add("Wayne");
            list.Add("Shane");
            list.Add("Susan");
            list.Add("Xavier");
 
            // There whould be 7
            Console.WriteLine(list.Count);
 
            // Use a public function to filter the list
            FilteredList filteredList = list.Filter(list.FilterFunction);
            //There should only be 2 stating with 'S'
            Console.WriteLine(filteredList.Count);
            foreach (string name in filteredList)
            {
                Console.WriteLine(name);
            }
 
            // Use one version of a lambda expression
            FilteredList filteredList2 = list.Filter(item => { return item.StartsWith("A"); });
            // Only one starting with 'A'
            Console.WriteLine(filteredList2.Count);
            foreach (string name in filteredList2)
            {
                Console.WriteLine(name);
            }
 
            // A better/shorter version of the previous lambda?  Definately cleaner
            FilteredList filteredList3 = list.Filter(item => item.StartsWith("Pi"));
            // Only one starting with 'Pi'
            Console.WriteLine(filteredList3.Count);
            foreach (string name in filteredList3)
            {
                Console.WriteLine(name);
            }
            
            Console.ReadLine();
        }
 
    }
 
    public class FilteredList : List<string>
    {
        public bool FilterFunction(string item)
        {
            return item.StartsWith("S");
        }
 
        public FilteredList Filter(Func<string, bool> condition)
        {
            FilteredList filteredList = new FilteredList();
            foreach (string item in this)
            {
                if (condition(item))
                {
                    filteredList.Add(item);
                }
            }
            return filteredList;
        }
    }
}

The result:

image

 

Here are my results for Map:

using System;
using System.Collections.Generic;
 
namespace TestMap
{
    static class Program
    {
        static void Main(string[] args)
        {
            VehicleList list = new VehicleList();
            list.Add(new GWiz());
            list.Add(new SmartCar());
            list.Add(new CityCar());
            list.Add(new PeopleMover());
 
            List<int> mappedList = list.Map(list, list.MapFunction);
            foreach (int numberOfSeats in mappedList)
            {
                Console.WriteLine(numberOfSeats);
            }
            Console.ReadLine();
        }
    }
    public class VehicleList : List<Vehicle>
    {
        public int MapFunction(Vehicle item)
        { 
            return item.NumberOfSeats;
        }
        public List<int> Map(VehicleList list, Func<Vehicle, int> mapFunction)
        {
            List<int> mappedList = new List<int>();
            foreach (Vehicle item in list)
            {
                mappedList.Add(mapFunction(item));
            }
            return mappedList;
        }
    }
    public abstract class Vehicle
    {
        public int NumberOfSeats { get; set; }
    }
    public class GWiz : Vehicle
    {
        public GWiz()
        {
            NumberOfSeats = 1;
        }
    }
    public class SmartCar : Vehicle
    {
        public SmartCar()
        {
            NumberOfSeats = 2;
        }
    }
    public class CityCar : Vehicle
    {
        public CityCar()
        {
            NumberOfSeats = 5;
        }
    }
    public class PeopleMover : Vehicle
    {
        public PeopleMover()
        {
            NumberOfSeats = 9;
        }
    }
}

 

The result will be: