tags:

views:

49

answers:

4

I know this question has a similar title to this: C++: calling member functions within constructor? but I am asking a more general question.

Is it good practice to call member functions from within a constructor? It makes reading the code easier and I prefer the encapsulation type way of doing it (ie. each block of code has a single objective).

An illustrative example, in python:

class TestClass:
    def __init__(self):
        self.validate()

    def validate(self):
        # this validates some data stored in the class

Is this a better way of doing it than writing the validate code inside the constructor? Are there drawbacks to this method? For example is it more costly with the function overhead?

I personally prefer it for readability but that's just my preference.

Cheers

A: 

I'm more familiar with C++ than Python, but I see no problem with calling member functions from constructors, especially when this practice is able to factor out similar code from multiple constructors. Anything that reduces redundancy is good in my books.

Reinderien
+2  A: 

I don't think there is anything inherently wrong in calling member functions from a constructor provided that they are not virtual functions.

The problem with calling virtual member functions from a constructor is that a subclass can override the function. This will cause the constructor to call the overridden implementation in the subclass, before the constructor for the subclass part of the object has been called.

In Java, any one of the private, static or final access modifiers will make the method safe to call from a constructor by preventing a virtual call to the superclass method. I don't think these techniques are available in Python.

richj
Actually, calling virtual methods from a constructor can be very useful. For example a virtual method to get back a specific class type to instantiate in the base constructor where the specific class is a descendant of a known base class or the implementor of a known interface. That way you can keep instantiation and destruction in the base class while allowing for customized composition by descendants.
Marjan Venema
Do you recommend "calling a method on an uninitialized object" as good practice for general purpose use across languages? I would prefer to use the AbstractFactory and/or Builder design patterns to solve your example problem.
richj
A: 

From a readability point of view it is definitely better. One thing you might have to ask yourself here though is whether the validate method is allowed to run after the object is initialized. If that is not the case, you can a) use some kind of private initialized variable or b) use the Builder pattern to get your objects into a valid state before using them.

Make sure the function is private. You do not want to mess with subclasses overriding it (Unless this is desired by design, in which case make it abstract/virtual).

Denis 'Alpheus' Čahuk
A: 

The main problem with this is that the member function has to work with an object that may be only partially initialized. And if it (even accidentally) passes a reference to the object somewhere else, other code has to od the same. This can get pretty confusing and error-prone, especially once you start overriding such a function in a subclass.

So in general, this practice should be avoided or at least confined to functions that can't be overriden, and they should never pass a reference to the object being constructed to any other code.

Michael Borgwardt