Declaring and initializing object of type child class in its base class

38 Views Asked by At

What's the name of the code smell committed by the following code. I try to google around to find the de-facto name of the following code smell to no avail.

The reason why I'm searching for it is because I saw someone committed this sin in their code and I would like to convince them that's a code smell by showing them an article that discussed about this code smell.

Nowadays it's hard to convince someone by just verbally telling them they are wrong with reason, I have to send them an online article that tell them they are wrong.

public class Base
{
   public Execute(string params)
   {
      Child c = new Child();
      c.Run(params);
   }
}
public class Child : Base
{
    void Run(string params)
    {
      //Do something
    }
}
1

There are 1 best solutions below

0
jaco0646 On

One service directly instantiating another violates the Dependency Inversion Principle, regardless of whether the instantiated class is a child class or unrelated to the instantiator.

The case of a parent --> child instantiation has the additional smell of a cyclic dependency because each class references the other.