DAGDA SEARCH | Back to results | Support

Chain of ‘is’ tests — CodeQL query help documentation

Source: https://codeql.github.com/codeql-query-help/csharp/cs-chained-type-tests/

Chain of ‘is’ tests

ID: cs/chained-type-tests
Kind: problem
Security severity: 
Severity: recommendation
Precision: high
Tags:
   - reliability
   - performance
   - changeability
   - language-features
Query suites:
   - csharp-security-and-quality.qls

Click to see the query in the CodeQL repository

Long sequences of type tests are primarily used to dispatch to different bits of code based on the type of a variable, as shown in the example later in this topic. They are often used to simulate pattern-matching in languages that do not support it. Whilst they do work as a dispatch method, they have a number of disadvantages:

Recommendation

There are a number of possible solutions to the problem, depending on the exact circumstances:

Example

This example uses a series of chained is statements to perform a different action depending on what kind of Animal is being iterated over.

class ChainedIs
{
    interface Animal { }
    class Cat : Animal { }
    class Dog : Animal { }

    public static void Main(string[] args)
    {
        List<Animal> animals = new List<Animal> { new Cat(), new Dog() };
        foreach (Animal a in animals)
        {
            if (a is Cat)
                Console.WriteLine("Miaow!");
            else if (a is Dog)
                Console.WriteLine("Woof!");
            else
                throw new Exception("Oops!");
        }
    }
}

Polymorphism is illustrated in the example below.

class ChainedIs
{
    interface Animal
    {
        void Speak();
    }
    class Cat : Animal
    {
        public void Speak() { Console.WriteLine("Miaow!"); }
    }
    class Dog : Animal
    {
        public void Speak() { Console.WriteLine("Woof!"); }
    }

    public static void Main(string[] args)
    {
        List<Animal> animals = new List<Animal> { new Cat(), new Dog() };
        foreach (var a in animals)
            a.Speak();
    }
}

Here is the same example again using the visitor pattern. This is a better solution if the idea of animal noises should be separate from the idea of animals.

class ChainedIs
{
    interface Visitor
    {
        void Visit(Cat c);
        void Visit(Dog d);
    }
    class SpeakVisitor : Visitor
    {
        public void Visit(Cat c) { Console.WriteLine("Miaow!"); }
        public void Visit(Dog d) { Console.WriteLine("Woof!"); }
    }
    interface Animal
    {
        void Accept(Visitor v);
    }
    class Cat : Animal
    {
        public void Accept(Visitor v) { v.Visit(this); }
    }
    class Dog : Animal
    {
        public void Accept(Visitor v) { v.Visit(this); }
    }

    public static void Main(string[] args)
    {
        List<Animal> animals = new List<Animal> { new Cat(), new Dog() };
        foreach (var a in animals)
            a.Accept(new SpeakVisitor());
    }
}

More details on reflection and the use of dynamic can be found in the references.

References