tags:

views:

31

answers:

1

I've got some inherited code that has a tendency to pass objects around as interfaces (IFoo, for example) then, at arbitrary places in the code, spontaneously cast them to concrete implementations of those interfaces (say, MyConcreteFoo).

Here's a silly example:

public bool IsThisFooScaredOfMonkeys(IFoo foo)
{
    if (foo is MyConcreteFoo)
    {
        return ((MyConcreteFoo)foo).BelievesMonkeysAreEvil;
    }
    return false;
}

What I'd like to do is write an NDepend CQL query to pick up these sorts of casts and give me a count per method, or per type, or anything really. Just something so I know where I can start focusing my efforts on getting rid of this particular brand of silliness, rather than sending my team spelunking through the code on a random hunt for casts...

Does anyone know if there's a way to do that? I'm guessing not (there can't be too many people out there who need that particular functionality) but I figured I'd ask here first... :-)

Of course, any other ideas on ways to make the cast-hunting go faster would be equally appreciated.

+2  A: 

This would be very nice, but NDepend is limited to a set of entities which does not cover individual statements.

NDepend Entities

  1. Methods
  2. Fields
  3. Types
  4. Namespaces
  5. Assemblies

Despite this limitation NDepend is still pretty awesome! Perhaps this is a version next feature.

Now Patrick Smacchia might be able to tell me different, so I would contact him with this question. I would expect to get a response back quickly as he is pretty on top of things.

On A Side Note:

If you are using ReSharper 5.0 it has a Structural Search that would allow you to find statements like this. You would have to build the search yourself, but it is a pretty powerful tool.

This pattern would catch the example above:

if($fooObject$ is $concreteFoo$)
{
    return (($concreteFoo$)$fooObject$).$anyIdentifier$;
}
Josh
I do indeed have ReSharper 5.0, and the structural search link gave me my answer. For anyone else who's curious, I set up a couple searches, one for finding "is" and one for finding "as" (there's probably a way to combine them, but this works for me).Search text is "$foo$ as $fooClass$", then add a placeholder of type Expression for "foo" that is of type IFoo or derived type, and a placeholder of type Type for "fooClass" that is also of type IFoo or derived type. Voila, I've got a list of all the places we cast.Thanks Josh, this is going to make my life a lot easier!
canuckotter