Constructor should generally not call methods

Posted by Stefano Borini on Programmers See other posts from Programmers or by Stefano Borini
Published on 2011-02-17T15:35:36Z Indexed on 2011/03/20 0:17 UTC
Read the original article Hit count: 288

Filed under:

I described to a colleague why a constructor calling a method can be an antipattern.

example (in my rusty C++)

class C {
public :
    C(int foo);
    void setFoo(int foo);
private:
    int foo;
}

C::C(int foo) {
    setFoo(foo);
}

void C::setFoo(int foo) {
    this->foo = foo
}

I would like to motivate better this fact through your additional contribute. If you have examples, book references, blog pages, or names of principles, they would be very welcome.

Edit: I'm talking in general, but we are coding in python.

© Programmers or respective owner

Related posts about design