Skip to content

A parameter by any other name (best practices with lambda expressions)

17-Jun-10

Lambda expressions are powerful tools that allow you to create succinct inline functions. However, without proper care they can quickly descend into an unreadable mess. While there are several ways that a lambda expression can become messy, this article will only address the naming conventions used for the expression’s parameters. Here are the two different conventions:

The single letter

When lambda expressions were first introduced, all of the code examples that were provided used single letters for their parameter names. I believe this was done to show the new compact syntax provided by lambda expressions.

In simple cases, a single letter is all that is needed:

Enumerable.Range(0, 10)
    .Where(i => i > 5)

In other cases a single letter can easily hurt the readability of the expression:

Enumerable.Range(0, 10)
    .Aggregate((m, n) => m + n)

In this example the meaning of the parameter names are hard to determine because we have used single letters for multiple parameters. This prevents us from knowing which parameter represents the sequence item and which parameter represents the aggregated state. In the first example it was easier to deduce the meaning of i because a single parameter indicated a single item in the sequence.

The longer and descriptive

After some time, I began to notice a shift in style (mainly in blogs and code that I read) – perhaps in response to the confusion caused by the single letter approach. People began to give more expressive names to their lambda expression parameters, even at the expense of the brevity of the expressions themselves.

Our Aggregate example from above becomes much easier to understand using this newer style:

Enumerable.Range(0, 10)
    .Aggregate((aggregated, current) => aggregated + current)

However this style applied to our first example (using Where) adds length to the expression without adding much else:

Enumerable.Range(0, 10)
    .Where(number => number > 5)

So what is the right way?

As in most things in life, the right way lies somewhere in the middle. Our first responsibility when it comes to naming conventions is to follow a set of rules that maximizes the readability of the code for those who will read it later. You do not want to write expressions that are cryptic and not easily understood in a single pass. You also do not want to write verbose expressions that are impossible to comprehend in their entirety.

Sometimes a single letter will suffice and other times a more expressive name is better. Here is a good rule of thumb:

If the expression requires one parameter then use a single letter. If the expression requires more than one parameter then use a more expressive identifier

Other thoughts

Another convention I have seen is the use of a single underscore as a placeholder for variables that are ignored in the body of the expression:

foo.SomeEvent += (_, args) =>
{
    // ...
};

In this example we are handling a standard EventHandler, but we don’t use the Object sender parameter. Rather than clutter the area with a name that looks meaningful, but isn’t used, it is better to use a single underscore as a placeholder. This satisfies the compiler’s requirement for an identifier, while indicating to the reader that the parameter is unused.

If in the event you do not need any of the parameters, rather than use placeholder identifiers, remember that C# 2 gave us the ability to create delegates that ignore the parameters altogether:

foo.SomeEvent += delegate
{
    // ...
};

Here’s to better parameter names in lambda expressions! Let’s try to write code today that we can all read tomorrow.

Two structural additions to C#

28-Apr-10

In What’s in a name? (C#’s hidden support for structural typing) we explored a few of the areas in which C# currently supports structural typing. I would like to propose two new additions to C#’s structural typing abilities: implicit interface inheritance and richer generic constraints.

Implicit interface inheritance

I propose that class or struct ought to be able to implicitly inherit from an interface if the two are structurally equivalent in every way. The idea is that if a type has all the members of an interface it can claim implicit inheritance of that interface making examples like this legal:

using System;
using System.Collections;
interface IName { String Name { get; } } class Example { static void Main() { printName(new { Name = "Nic" }); } static void printName(IName name) { Console.WriteLine(name.Name); } }

Allowing any type to implicitly implement an interface would open many opportunities. First, it would encourage the use of anonymous types for message passing between methods thereby avoiding the need to declare a type.

Second, now the fact that interfaces would offer even more flexibility, developers would be encouraged to choose interfaces for their method parameter types (i.e. program to an interface).

Richer generic constraints

Currently, C# only supports new() as a generic constraint for type constructors. This constrains the type parameter to only those types that have parameterless constructors. This means that if you want to instantiate an instance of a generic type argument you are forced to use reflection to do so:

using System;
class Foo { public Foo() { } public Foo(String name) { } } class Example { static void Main() { Foo f = create<Foo>(); } static T create<T>() where T : new() { Object instance = Activator.CreateInstance(typeof(T), new[] { "Foo" }); return (T)instance; } }

While Activator.CreateInstance is a very useful method, it does use reflection and any incompatibility between the constructor of the type and the arguments passed to it will cause an exception to be thrown. I propose that C# incorporate a new type of generic constraint for constructors that allow developers to specify the signature of the constructor like this:

using System;
class Foo { public Foo() { } public Foo(String name) { } } class Example { static void Main() { Foo f = create<Foo>(); } static T create<T>() where T : new(String) { return new T("Foo"); } }

If the generic type argument has a constructor that matches the structure of the generic type constraint it should satisfy the constraint. This will allow richer generic programming as we will be able to instantiate many different types of objects without sacrificing type-safety.

So what do you think? Are there other areas that C# could implement structural typing that would be beneficial?

What’s in a name? (C#’s hidden support for structural typing)

27-Apr-10

C# is well known for its nominative type system. This means that C# identifies types and their relationships based on their names. That is why C# does not allow you to create two types with the same name, even if they have a different public interface – the C# compiler only cares about the name of the type.

There is another type system called a structural type system. A structural type system recognizes the relationships between types based on the structure (i.e. the public members) of those types – names do not come into consideration. For example, if C# had a structural type system, these two classes would be considered compatible:

class Foo
{
    public String Name { get; set; }
}
class Bar
{
    public String Name { get; set; }
}

Because both of these types are declared with a String property called Name, a structural type system would happily work with both of these types as if only one type were declared. This means that instances of Foo could be assigned to a reference typed as Bar and instances of Bar could be passed to methods expecting a argument of type Foo. (This is similar to duck typing except that duck typing binds member calls at execution time whereas a structural type system does static type checking at compile time.)

Many developers do not realize that C# already supports structural typing in certain scenarios (most of them are well-concealed to reduce confusion). Here is the most common place where C# employs structural typing concepts:

foreach (Object o in someCollection) { }

Did you know that when the C# compiler encounters a foreach in your code it uses a structural approach to determine whether or not what you are enumerating a valid collection?

Consider this example:

class Foo { }
class Example { static void Main() { Foo foo = new Foo(); foreach (var f in foo) { } } }

This code does not compile, but fortunately the compiler’s error message gives us some indication of how to fix the problem:

foreach statement cannot operate on variables of type ‘Foo’ because ‘Foo’ does not contain a public definition for ‘GetEnumerator’

Notice that the compiler doesn’t say that we ought to implement IEnumerable – rather it tells us that our Foo class doesn’t provide a GetEnumerator method. This is because the compiler is only concerned about the structure of Foo, not it’s nominative relationships to an interface that would ensure that the GetEnumerator method was present.

Let’s fix the error by adding a GetEnumerator method to Foo like the compiler requested:

using System.Collections;
class Foo { public IEnumerator GetEnumerator() { return null; } } class Example { static void Main() { Foo foo = new Foo(); foreach (var f in foo) { } } }

Interestingly enough, this does compile – structural typing at work!

Here is another example of C#’s existing support for structural typing:

var list = new List<String> {
    "hello", "world"
};

When the C# compiler encounters a collection initializer like this one, it uses a structural approach to determine if the type that is being initialized is actually a collection. Rather than using a nominative approach and checking to see if the type implements ICollection<T>, the compiler actually only checks to see if the type implements IEnumerable<T> (a nominative type check) and if the type exposes a public Add method. It is this last check for Add that is structural in nature since the compiler is checking for the presence of a method, not a nominative relationship on the type itself.

The purpose of this article was to show two areas where C# supports structural typing. I would be interested in discussing any other hidden areas of C# that are structural in nature – are you familiar with any that I didn’t cover?

Stay tuned for a follow-up article discussing two structural features I would like to see implemented in C# 5.

Edit: Here is the follow up article: Two structural additions to C#.

Choosy developers choose interfaces

23-Apr-10

Each time you write a parametrized method you must decide which are the best types to use for your parameters. If you are not careful, your choice could place unnecessary restrictions on the caller of the method that requires them to pass too specific of a type. When you are making this decision you should ask yourself the following question:

Is there an interface that encapsulates all the members that I need for the type of this parameter?

Here is the classic example of this problem:

class Example
{
    public void Print(List<String> values)
    {
        foreach (String value in values)
            Console.WriteLine(value);
    }
}

This Print method was not written with the caller in mind – these kinds of methods are often written with a narrow focus on the needs of the developer who wrote the method. This developer is used to using a List<T> for any sequence of items so he naturally chooses it as his parameter type for this method. But is it the right choice?

Since this article is about choosing interfaces I imagine that you rightly guessed not. In order to choose the correct type for the argument you need to first find the least restrictive type you can that will allow you to accomplish everything you need to do to satisfy the requirements of the method.

Here is the process for finding the best type for your parameter. Start by looking at the implementation of the type you want to use. List the methods and properties you use from it. Next determine if it implement any interfaces. If it does, does any one interface provide all the members your method needs to function? If so, repeat this exercise but this time look at the implementation of the interface you have chosen. Does it inherit from another interface? Is it that interface that actually provides the members you need? Keep doing this until you have reached the highest level of abstraction that you can. Once you cannot go any further you have found the right type for your parameter.

In the case that your type does not implement any interfaces or the interfaces it does implement cannot be used as the parameter type (in other words the exercise in the previous paragraph failed) you will have to rely on a concrete type for your parameter. At this point you must repeat the exercise but do so using the inheritance hierarchy of types above your type.

After you have completed this exercise you can be confident that you have chosen the most abstract choice possible for your parameter and therefore you are providing maximum flexibility for the callers of your method.

Let’s apply this process to a new method that removes all instances of “1″ from the Text property of an ASP.NET textbox:

void removeOnes(TextBox textbox)
{
    textbox.Text 
        = textbox.Text.Replace("1", "");
}

Here is the mental process that I would go through to choose the proper type for this method:

  • This removeOnes method uses just one member from TextBox and that is TextBox.Text.
  • TextBox implements two interfaces: IPostBackDataHandler and IEditableTextControl.
  • Does any one interface provide all the members your method needs to function? Yes, IEditableTextControl declares a Text property.
  • Does IEditableTextControl inherit from any other interface and if so does that interface provide a Text property? Yes, IEditableTextControl inherits from ITextControl which does provide a Text property.
  • Does ITextControl inherit from any other interface and if so does that interface provide a Text property? No, this is as far as we can go.

We have just found the most abstract interface that we can possibly use and that means that it is the best type to use for our parameter:

void removeOnes(ITextControl textControl)
{
    textControl.Text 
        = textControl.Text.Replace("1", "");
}

Now that we have changed the type of the parameter to ITextControl we have now given much more flexibility to the caller of this method. We have also improved the reusability of this method since it can now be used for any control that implements the ITextControl interface (e.g. TextBox, Label, Literal and many more).

Can anyone tell me what type should have been used for the first example? What if the implementation of the first example changed a bit (as I am sure that everyone was able to quickly identify the proper type for the first example).

public void Print(List values)
{
    for (Int32 i = 0; i < values.Count; i++)
        Console.WriteLine(values[i]);
}

Can you list what members of List<T> are being used by Print now? With that in mind, what type would you choose for values?

Writing better code: it’s imperative that you are declarative

21-Mar-10

Most C# developers are very familiar with writing imperative code (even though they may not know it by that name). In this article, I will introduce you to an alternative style of programming called declarative programming. Proper declarative code is easier to read, understand, and maintain.

As professionals, we should be striving to write better code each day. If you cannot look at code you wrote three months ago with a critical eye and notice things that could be better, then you have not improved and are not challenging yourself. I challenge you to write code that is easier to read and understand by using declarative code.

First, it is important to understand what declarative code is and how it relates to imperative code.

Imperative code describes how something is done whereas declarative code describes what is being done.

Imperative code is generally difficult to read and understand. For example:

using System;
class Example { static void Main() { Int32 sum = 0; for (Int32 i = 0; i < 100; i++) { if (i % 2 == 0) { sum += i; } } Console.WriteLine(sum); } }

It is not immediately apparent what this code does. Only through careful examination can we deduce that it prints the sum of all even numbers between 0 and 99.

This is how the same program would be implemented using a declarative style:

using System;
using System.Linq;
class Example { static void Main() { Int32 sum = Enumerable.Range(0, 99) .Where(i => i % 2 == 0) .Sum(); Console.WriteLine(sum); } }

Obviously the second example is different, but is it better? I believe it is. We have condensed the example into a single expression and the expression is significantly easier to understand. The name of each method is used to express the intention of that portion of the program. Rather than looping with a classic for loop I have used the newer Enumerable.Range method. This not only better expresses my intentions, but also gives me a starting place from which I can easily stream the numbers through a filter (the Where method) and finally aggregate them with Sum.

But I still think this code could be better. Let me do one last thing to make our code even more declarative:

using System;
using System.Linq;
class Example { static void Main() { Int32 sum = Enumerable.Range(0, 99) .Where(isEven) .Sum(); Console.WriteLine(sum); } static Boolean isEven(Int32 number) { return number % 2 == 0; } }

This change is subtle, but important. We have moved the somewhat cryptic expression that tests for evenness into its own method. Since this method has a single responsibility and is clearly named, it is ideal for inclusion in our declarative expression. It is important to understand that declarative code doesn’t necessarily mean less code. Declarative code is characterized by how expressive it is. By moving the test for evenness into its own method we may have increased the line count of the program, but we have also greatly improved the readability of the code as well.

I hope that I have shown how you can improve your code by making it more declarative. If you strive to write more declarative code you will end up with better software that is easier to read, understand, and maintain.

All types are not compared equally (part 2)

18-Mar-10

In part 1 of this series I explained the difference between reference equality and value equality. In this article I am going to demonstrate how to compare two reference types using value equality semantics.

Override Object.Equals

Every time you use the binary equality operator (==) or the Equals method on a reference type you are invoking Object.Equals for the instances in question. If you wish to provide value equality the most obvious thing to do would be to override System.Object.Equals and use this method to compare the fields of your two instances. Let us begin by revisiting our Person type which I have refactored to make these examples a bit more interesting:

class Person
{
    public String Name { get; set; }
    public DateTime Birthday { get; set; }
}

We have a class with two properties – let’s override the Equals method:

class Person
{
    public String Name { get; set; }
    public DateTime Birthday { get; set; }
public override Boolean Equals(Object obj) { Person other = (Person)obj; return this.Name == other.Name && this.Birthday == other.Birthday; } }

Now we have value equality for our reference type. While this is a simple solution, it is not ideal for the following reasons:

  1. This approach is not type safe. Since the Equals method accepts an argument of type Object we cannot guarantee that the instance that was passed to this method is actually a Person.
    somePerson.equals("hello, world");  // throws an InvalidCastException
  2. This approach is not “null safe”. Any comparisons with null will throw a NullReferenceException.
    somePerson.equals(null); // throws a NullReferenceException

The null safety issue is easy enough to fix:

public override Boolean Equals(Object obj)
{
    if (obj == null)
        return false;
Person other = (Person)obj; return this.Name == other.Name && this.Birthday == other.Birthday; }

But how do we preserve type safety? Meet the IEquatable<T> interface.

IEquatable<T>

This interface was designed specifically to help us tackle the type safety issue that we are facing. It declares a single member:

public interface IEquatable<T>
{
    Bolean Equals(T other);
}

As you can see, this interface gives us the ability to create a strongly-typed override of our existing Equals method. Implement the interface like this:

class Person : IEquatable<Person>
{
    public String Name { get; set; }
    public DateTime Birthday { get; set; }
public override Boolean Equals(Object obj) { return this.Equals(obj as Person); } public Boolean Equals(Person other) { if (other == null) return false;
return this.Name == other.Name && this.Birthday == other.Birthday; } }

Now that we have a strongly-typed Equals method any equality comparisons that are done on two instances of our type will be type-safe and null-safe. Using the as cast in the default overridden implementation of Equals allows us to pass either an instance of Person or null and our implementation of IEquatable<T>.Equals returns false which ensures that our methods won’t fail for null (for more information on the as operator, please read Two casts are not better than one).

GetHashCode

I am not going to cover the GetHashCode method in depth but it is an essential part of identity equality checks. A hash code is an integral value that represents the state of the current instance. Basically, if two instances have the same hash code, they may be equal in terms of value. But if two objects do not have the same hash code they are most certainly not equal in terms of value. This method allows our calling code a performance boost by not having to call Equals in the event that the hash codes do not match.

As for the proper or best way to generate a hash code for an object instance, that is a discussion for another day. For now I will add a simple GetHashCode implementation to our example to complete the exercise. (Thank you to Jon Skeet for this particular implementation of GetHashCode.)

class Person : IEquatable<Person>
{
    public String Name { get; set; }
    public DateTime Birthday { get; set; }
public override Boolean Equals(Object obj) { return this.Equals(obj as Person); } public Boolean Equals(Person other) { if (other == null) return false;
return this.Name == other.Name && this.Birthday == other.Birthday; } public override Int32 GetHashCode() { Int32 hash = 23; hash = hash * 37 + this.Name.GetHashCode(); hash = hash * 37 + this.Birthday.GetHashCode(); return hash; } }

All we are doing here is taking two coprime numbers (23 and 37) and using them to manipulate the hash codes of our instance’s state in order to arrive at a final integral value. Again, how the implementation works is not important at this point, what is important is that we are providing some implementation so that we can reap the performance benefits that GetHashCode can provide. I will post an article in the future that discusses the different techniques of generating hash codes and why some implementations are better than others.

Conclusion

Now we have a class that properly provides value equality semantics. I hope I have showed not only how to implement this pattern in your code but also why it is important and necessary in the first place.

Edit: Bradley Grainger correctly points out in a comment below that I have neglected to provide equality operator overloads for my Person type! Here is a complete example that includes those operator overloads:

class Person : IEquatable<Person>
{
    public String Name { get; set; }
    public DateTime Birthday { get; set; }
public override Boolean Equals(Object obj) { return this.Equals(obj as Person); } public Boolean Equals(Person other) { if (other == null) return false;
return this.Name == other.Name && this.Birthday == other.Birthday; } public override Int32 GetHashCode() { Int32 hash = 23; hash = hash * 37 + this.Name.GetHashCode(); hash = hash * 37 + this.Birthday.GetHashCode(); return hash; } public static Boolean operator ==(Person left, Person right) { // If the object's have the same reference then they are most // certainly equal - return true here. if (Object.ReferenceEquals(left, right)) return true;
// Check for null here as well - make sure you cast the references // to Object so that you don't accidentally invoke this same operator // again. Casting to Object allows you to invoke Object's == operator. if ((Object)left == null || (Object)right == null) return false;
return left.Equals(right); } public static Boolean operator !=(Person left, Person right) { return !(left == right); } }

Make aliases an option

26-Nov-09

Do you prefer int or Int32? How about string or String? Some developers have strong preferences, others don’t care. Personally, I prefer to use the actual CLR type names (in other words I prefer Int32 and String) and since I do, I feel as though I am a second-class citizen.

Interestingly enough, it is Visual Studio who makes me feel this way! I am constantly frustrated when I use intellisense to auto-populate an object instantiation like this:

Dictionary<String,Int32> dictionary = new

If I use intellisense to populate the remaining code I am left with this:

Dictionary<String,Int32> dictionary = new Dictionary<string,int>();

This happens with any code that Visual Studio generates on my behalf: method overrides, method stubs, etc. It would be really nice to be able to specify that I prefer to use the CLR type names over their C# aliases. So I am making an official Visual Studio feature request:

Please allow us to specify whether or not suggested and generated code uses C# aliases or CLR type names.

Those of us who care will greatly appreciate it.

Smarter type inference with C# 4

03-Nov-09

Here is a new addition to the C# language in version 4 that doesn’t seem to be getting much attention. The type inference algorithm has become smarter and for the first time we are able to infer generic type arguments from the return types of methods in certain cases.

Consider this example that compiles in C# 4 but does not compile in C# 3:

using System;
using System.Linq;
class Example { static void Main() { var characters = new [] { 97, 98, 99 } .Select(Convert.ToChar); } }

Here I am converting an array of integers to a new sequence of characters. In order to do this I chose to project this new sequence from the old sequence using LINQ’s Enumerable.Select<TSource,TResult> extension method. Now if I were using C# 3 I would have to explicitly provide type arguments for the Select method since the compiler would be unable to infer the types from the usage alone.

Note: This was a follow-up to You don’t always need a lambda expression. As you can see this new functionality gives us many more opportunities to forgo a lambda expression since we are no longer limited to Action delegates.

You don’t always need a lambda expression

01-Nov-09

C# 3 gave us the lambda expression and ever since we have been using them to create abstract representations of terse, anonymous functions.

While expressive and helpful, they are not always needed. Consider this example:

using System;
using System.Collections.Generic;
class Example { static void Main() { new List<Int32> { 1, 2, 3 } .ForEach(i => Console.WriteLine(i)); } }

The preceding code can be refactored to this:

using System;
using System.Collections.Generic;
class Example { static void Main() { new List<Int32> { 1, 2, 3 } .ForEach(Console.WriteLine); } }

What is different? In the second example we are simply passing the name of the method group that we wish to invoke. Since List<T>.ForEach accepts a single parameter of type Action<T> we can leverage the flexibility afforded us by the C# compiler to pass a method group name (in this case Console.WriteLine) to this method. The compiler helpfully creates the new Action<Int32> delegate behind the scenes.

In this instance refactoring doesn’t improve performance (in fact, the resulting IL is identical), but it does present a more readable expression (it is in fact an excellent example of declarative programming). So I challenge you to find opportunities to forgo lambda expressions. If you find yourself writing lambda expressions that look similar to the one I have used in the example, stop and evaluate it carefully to see if you really need it.

You will find this mental exercise rewarding as it will not only improve the expressivity of your code, but it will also force you to think carefully about its functional nature.

Caveat: Since C# 3 does not support the ability to infer generic type arguments from the return types of method groups, this really only works for examples like I have posted. They work because the examples allow type inference to be accomplished based on the types of the method’s arguments.

In other words, C# 3 will only let you do this for methods that accept delegates in the Action family (i.e. Action, Action<T>, etc.) since these delegates do not return any values. Revisions to the type-inference algorithm in the C# 4 compiler changes this and opens up a lot of possibilities. Check back soon for another article that will cover this topic in depth.

Edit: Here is the follow-up article: Smarter type inference with C# 4.

All types are not compared equally

29-Oct-09

Here is a bit of a quiz: What does this program print?

using System;
using System.Collections.Generic;
class Person { public String Name { get; set; } }
class Example { static void Main() { var people = new List<Person> { new Person { Name = "George Washington" } };
Console.WriteLine( people.Contains( new Person { Name = "George Washington" })); } }

If you said “True” then unfortunately you would be incorrect – this program prints “False”. Before I explain why this program prints “False” I would like to make one slight change to this program to make it print “True”.

using System;
using System.Collections.Generic;
struct Person { public String Name { get; set; } }
class Example { static void Main() { var people = new List<Person> { new Person { Name = "George Washington" } };
Console.WriteLine( people.Contains( new Person { Name = "George Washington" })); } }

Did you spot it?

If you look carefully you will notice that I changed the Person type to be a struct instead of a class. Why would this make a difference? In order to understand let’s take a brief tour of how C# (and the CLR) handle equality.

Whenever you create a type without overriding the Equals method or implementing the IEquatable<T> interface you relinquish control over how two instances of your type will be compared and accept the CLR’s default comparison mechanisms. If your type is a reference type (a class) you will get identity equality and if your type is a value type (a struct) you will get value equality.

Identity equality

Reference types are called reference types because the object itself is stored on the managed heap and you access that object via a reference to that object.

When you compare two instances of a reference type (like the Person type in my first example) the CLR will compare the references to the objects to see if the references point to the same object. Two reference types will only be equal if their reference point to the exact same object on the managed heap. If the references are different the objects are not equal then neither are the objects – even if their fields are identical.

Identity equality asks this question:

Do these two references point to the same object on the managed heap?

Value equality

Value equality is a different process but is actually much simpler to understand. Value equality takes all instance fields of a value type and compares them to the instance fields of a second instance in respective order. I would imagine that value equality works much the way most developers expect all equality checks ought to.

Value equality asks this question:

For each field in these two instances, are all the values equal?

So does this mean that in order to have value equality we must use value types? Of course not! I will soon publish a follow-up article explaining the proper way to implement value equality semantics with reference types.

Edit: Here is the follow-up article: All types are not compared equally (part 2).