1

In an object oriented module describing a database, should I pass DB description data structures to the constructor in the constructors of derived classes, or should I instead create ("virtual" that is dynamic) methods which create the data structures and call them in the constructor?

These data structures are long (around 100 elements arrays).

A Python example follows:

class Base:
  def __init__(data_desciption):
    self.data_desciption = data_desciption

vs

class Base:
  def __init__():
    self.data_desciption = self.data_desciption_init()

or in C++:

 class Base {
   DataDescription data;
 public:
   Base(DataDescription &data_description) {
     data = data_description;
   }
 };

vs

 class Base {
   DataDescription data;
 protected:
   virtual DataDescription &get_data_description() = 0;
 public:
   Base() {
     data = get_data_description();
   }
 };

(probably not optimized for speed, but this is now irrelevant). (oh, I've forgotten that C++ constructors can't call pure virtual functions. Not used C++ for a long time. But this does not change the essence of my question).

In fact I am now programming in Perl.

5
  • 2
    Could you give an example?
    – 9000
    Aug 28, 2015 at 17:57
  • @9000 I've added two Python example. Which of the two is better? If you don't understand Python, please suggest me to write in another language
    – porton
    Aug 28, 2015 at 18:04
  • @9000 I've also added C++ examples
    – porton
    Aug 28, 2015 at 18:10
  • I now think, I should use virtual methods rather than passing values to the constructor, because it eases making default values for the datastructures (I can just define a default method in the base class and override it only when I really need.)
    – porton
    Aug 28, 2015 at 18:29
  • Another argument for using virtual methods is that such methods can be easily overridden, for example adding 101st element to the 100 elements array created by the parent class
    – porton
    Aug 28, 2015 at 19:09

1 Answer 1

4

Passing in the data structure will prevent a potential bug that can occur when a the constructor of a base class calls an abstract method defined by the child class.

This potential bug occurs when the child class has a member variable that is initialized in the constructor. Then, the implementation of the abstract method assumes that member variable has been initialized. In many languages, the base class constructor is always executed at the start of the child class constructor. This means the base class calls the abstract method before the child class has a chance to initialize it's member variable.

In Python, this can be avoided by initializing the member variables before calling the base class constructor. But you still have to be aware of this potential issue in order to work around it. I don't know enough Perl to comment on if this work around could also be used.

1
  • I think it is absolutely irrelevant for Perl. (Following a good practice) I call init() method in the constructor and override this init() in derived classes. In the derived init() I call init() of the base class whereever I want
    – porton
    Aug 28, 2015 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.