Is it good practice for object APIs to be required to be called in sequence to gather information?

  softwareengineering

I had this discussion with someone and it ended ambiguously.

Suppose you have a class that needs to Parse a file to gather some information. It can expose this information to you after the fact.

Should all this functionality go in a constructor, or is it okay to have like:

<Construct the Object>
<Call Parse>

My coworker argued that since we’re leaving the “information” to be gathered uninitialized in the constructor, with it only being valid after the constructor, that all the functionality should go in the constructor.

I argued that you don’t want to fail in the constructor and have no way of letting the outside world know. He said we should throw an exception.

This can be generalized to any arbitrary API, where it requires you call some N number of functions in sequence to get your result. Should this work instead be done at construction?

New contributor

user129393192 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT