Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you consider multiple initialization steps "poor form"?

Tags:

c++

oop

I'm writing a physics simulation (Ising model) in C++ that operates on square lattices. The heart of my program is my Ising class with a constructor that calls for the row and column dimensions of the lattice. I have two other methods to set other parameters of the system (temperature & initial state) that must get called before evolving the system! So, for instance, a sample program might look like this

int main() {
    Ising system(30, 30);
    system.set_state(up);
    system.set_temperature(2);

    for(int t = 0; t < 1000; t++) {
        system.step();
    }

    return 0;
}

If the system.set_*() methods aren't called prior to system.step(), system.step() throws an exception alerting the user to the problem. I implemented it this way to simplify my constructor; is this bad practice?

like image 514
Connor Glosser Avatar asked Jan 11 '11 12:01

Connor Glosser


People also ask

Why should classes be designed so that they can be fully initialized by their constructors?

The constructors should be used to initialize member variables of the class because member variables cannot be declared or defined in a single statement. Therefore, constructors are used in initializing data members of a class when an object is created.

Why do we need dynamic initialization of objects?

Answer: Dynamic initialization of object refers to initializing the objects at run time i.e. the initial value of an object is to be provided during run time. Dynamic initialization can be achieved using constructors and passing parameters values to the constructors.

What is initialization in C++ with example?

[edit] Initialization of a variable provides its initial value at the time of construction. The initial value may be provided in the initializer section of a declarator or a new expression. It also takes place during function calls: function parameters and the function return values are also initialized.

What is initialization in Java with example?

In Java, an initializer is a block of code that has no associated name or data type and is placed outside of any method, constructor, or another block of code. Java offers two types of initializers, static and instance initializers. Let's see how we can use each of them.


3 Answers

It is recommended to put all mandatory parameters in the constructor whenever possible (there are exceptions of course, but these should be rare - I have seen one real-world example so far). This way you make your class both easier and safer to use.

Note also that by simplifying your constructor you make the client code more complicated instead, which IMO is a bad tradeoff. The constructor is written only once, but caller code may potentially need to be written many times more (increasing both the sheer amount of code to be written and the chance of errors).

like image 123
Péter Török Avatar answered Sep 29 '22 10:09

Péter Török


Not at all, IMO. I face the same thing when loading data from external files. When the objects are created (ie, their respective ctors are called), the data is still unavailable and can only be retrieved at a later stage. So I split the initialisation in different stages:

  1. constructor
  2. initialisation (called by the framework engine when an object is activated for the first time)
  3. activation (called each time an object is activated).

This is very specific to the framework I'm developing, but there is no way to deal with everything using just the constructor.

However, if you know the variables at the moment the ctor is called, it's better not to complicate the code. It's a possible source of headaches for anyone using your code.

like image 34
mingos Avatar answered Sep 29 '22 10:09

mingos


IMO this is poor form if all of these initialization steps must be invoked every time. One of the goals of well-designed software is to minimize the opportunities to screw up, and having multiple methods which must be invoked before an object is "usable" simply makes it harder to get right. If these calls were optional then having them as separate methods would be fine.

Share and enjoy.

like image 27