mercredi 19 avril 2023

I wrote a class with "init" method. Should I call it from other class methods? Or leave it to the object user? (Code Design) [closed]

I have a java class with an init method. It's different from the constructor. The constructor just initializes the variables/fields. The init method connects to a database and performs some verifications on the database + it kick starts a background thread which handles incoming requests.

If you wanna think of it in pseudo-code:

class SomeService:

    ...
    queue
    other fields
    ...

    public SomeService(Constructor input):
        initializes fields...

    public void init():
        connect to database and verify certain existence of a table
        start a thread
        initialized = true

    public String doSomething(Some Input):
        if !initialized: throw exception
        add job to queue to be picked up by the running thread

    public Integer doSomethingElse(Some Other Input):
        if !initialized: throw exception
        add job to queue to be picked up by the running thread


My question is, is it better to force the object user to call init before using the class methods? If you check the code above, any method will throw exception if it's called before init.

Benefits:

  • This provides separation of concerns -- a method is only responsible of doing its job.

Downside:

  • This, however, makes a bad class usability: what if init failed? the object user will always has to handle this. And might even try to call init before every other class method call.

The other option is call init at the beginning of every class method, internally.

Benefits:

  • The class user doesn't need to worry about anything but getting his services done by the object.

Downside:

  • Now a method isn't as good in terms of separation of concerns. It does its job after it tries to initialize the object.

Please note that the initialization procedure is a must before any other method does its job. Other methods won't be functional unless the object is initialized by calling init.

I believe it's obvious why I separated init from the constructor:

  1. It's perfectly fine to have the object without it being initialized, and leave initialization for later.
  2. A constructor shouldn't take long for initialization, which isn't the case. Connection to database, running multiple statements, and starting a thread. This isn't as snappy as just setting fields values.
  3. If connection to database failed in a constructor, this will result in no object creation. This is bad behavior because the database can be available some other time after trial of object instantiation.

What I did is:

  • Called init at the beginning of every method. To get the benefit of that.
  • Availed init as public method to permit object caller for eager initialization.

So, what is it do you think is better? Call init at the beginning of every method? Leave it completely for the object user to handle it?

I tried to look it up but didn't find a fruitful answer.

I also tried to find in java 8 standard library if any class has init method, but couldn't find any. However, I found in java extension, Cipher class, which has init. and it works like approach#1 (leave it to the caller). That being said, this makes sense because its init takes user input, while mine doesn't.

I asked a chatgpt based bot and I got an answer that I should go with approach#2, calling init at the beginning of every method.

Aucun commentaire:

Enregistrer un commentaire