Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a benefit or drawback when one constructor implements another?

Tags:

c#

constructor

If I have a class like this:

public class Foo
{
    public IEnumerable<Bar> Bars { get; set; }

    public Foo()
    {
        Bars = new List<Bar>();
    }
}

At some stage I re-factor the class and add a secondary constructor which implements the first one like this:

public class Foo
{
    public IEnumerable<Bar> Bars { get; set; }

    // some more properties were added

    public Foo()
    {
        Bars = new List<Bar>();
    }

    public Foo(string parameter): this()
    {
        .... some code here
    }
}

I could have also written it similar to this:

public class Foo
{
    public IEnumerable<Bar> Bars { get; set; }

    // some more properties were added too

    public Foo()
    {
        InitilizeFoo();
    }

    public Foo(string parameter)
    {
        InitilizeFoo();
        .... some code here
    }

    private void InitializeFoo()
    {
        Bars = new List<Bar>();
    }
}

Seeing both approaches work in this scenario, is there a benefit or drawback in using one over the other?

Is inheriting constrcutors more efficient and making that code execute faster or is there a drawback which I don't know about making the second implementation more efficient instead?

like image 881
Nope Avatar asked May 31 '12 17:05

Nope


People also ask

Why would you need to use more than one constructor?

To put it simply, you use multiple constructors for convenience (1st example) or to allow completely different initialization methods or different source types (2nd example. Show activity on this post. A class can have multiple constructors, as long as their signature (the parameters they take) are not the same.

Can we call one constructor from another constructor?

The invocation of one constructor from another constructor within the same class or different class is known as constructor chaining in Java. If we have to call a constructor within the same class, we use 'this' keyword and if we want to call it from another class we use the 'super' keyword.

What is the benefit of using constructor?

One of the benefits of using a constructor over a method is that you can be assured the constructor was called and the work within the constructor was performed. The language specifies that to construct an object a constructor must be called.

Can you have 2 constructors in Python?

Providing Multiple Constructors With @classmethod in Python. A powerful technique for providing multiple constructors in Python is to use @classmethod . This decorator allows you to turn a regular method into a class method. Unlike regular methods, class methods don't take the current instance, self , as an argument.


1 Answers

One of the key benefits in having one constructor call another constructor is that you can set read-only fields that way, you can't do that by calling a non-constructor method.

For example:

public class Foo
{
    private readonly int myNumber;

    public Foo() : this(42)
    {
    }

    public Foo(int num)
    {
        myNumber = num;
    }
}

Performance wise, it's probably no more or less efficient to call another constructor than to call another method, but it is more readable, in my opinion, for a constructor to call another constructor than to call a separate, private method whose only point is to be called by a constructor.

There could, of course, be situations when having a separate method makes sense, and it's certainly not "wrong" per se. Chaining constructors just reads better to many for most uses, and there is no negative performance impact.

UPDATE: I performed 10,000,000 iterations of each way (chained vs private initialization method) and the results were so close they were nearly indistinguishable:

Initializer Method took: 84 ms for 10,000,000 iterations, 8.4E-06 ms/each.
Chained Constructors took: 81 ms for 10,000,000 iterations, 8.1E-06 ms/each.

So really, performance-wise there is nearly no benefit either way. The main benefit is with chained constructors you can set readonly fields, and in most cases it is more readable.

like image 110
James Michael Hare Avatar answered Oct 27 '22 01:10

James Michael Hare