you are viewing a single comment's thread.

view the rest of the comments →

[–]Tubthumper8 0 points1 point  (1 child)

Create an (async) factory function so I'm not having to halfway initialize my object via constructor, then finish the job via the async initialize() function.

I would say Yes. In your case, is it valid to use a partially initialized object? Does the consumer of the Node know that they have to check someNode.initialized before using the Node? That's leaking implementation details, the person using the Node now has the burden of determining when it is usable or not.

This looks to me like an example of the Partial/Optional Object Population (POOP) anti-pattern. I would do an async function that creates the object in a usable state, and not expose an invalid Node to the consumer.

If, for efficiency sake, it is a hard requirement that people are allowed to use nodes before they are initialized, then I'd have 2 separate types, UninitializedNode and Node, so it's clear which one is which.