With each new version of .net framework Microsoft tries to add some new elements to the language, which in theory should improve the possibilities of language and comfort of the software developer. In .net 4.0 new keyword – [mark]dynamic[/mark] – has been introduced. This type enables the operations that will be resolved at run time. It means that we can use any name with dynamic object and compiler will not show any error during compilation – even if there is no method with used signature. The error will occur when the running application will not find used method.

Let’s back to code review… I thought that I have seen many weird things in code. But it seems that creativeness of software developers does not know limits. Last time I have found the following code.

There is nothing special on beginning. Code provides read only property, which was defined in interface:

interface IReadOnlyFoo
{
  int SomeValue { get; }
}

There was also a method using this object which implements this interface as an argument:

public void DoSomething(IReadOnlyFoo obj)
{
  ...
}

Till now everything looks normal. In most cases I would not look into DoSomething method. I would just use it. But last time I have that felling – it was saying: You should check this! Something is wrong! And I looked deeper to this method and I have found following code:

public void DoSomething(IReadOnlyFoo obj)
{
  ((dynamic)obj).SomeValue = 10;
}

Hmmmm… Surprise! In language with strong type control, I say that value of property will not be changed according to the method signature and in the same time I try to get to the object by using very sophisticated and brutal constructions that can guaranty only one thing – the compiler will not threat this construction as an error. What is more important, together with those construction we should be aware about great risk. It is high possible that when we will do some refactoring in IReadOnlyFoo interface we will cause serious error that will be revealed by user who will use our application.

I wonder what we can do with such situation. Maybe Microsoft could add to new version of language mechanism that allows defining list of key words that should not be used in project. And then each usage of one word from list should be approved by other team members.

Please let me know if you have better solution for this issue?