zserio
zserio copied to clipboard
when to call initialize
Ideally initialize() is called directly after object construction. However this leads to a performance overhead: the initialize() of underlying structs will also be called leading to a cascading effect. So currently I only call it if known to be needed (like if I need to use hashcode() or just before writing to the bitstream). This leads to code maintainability issues, because one needs know beforehand if such a function will be called. Calling initialize() before hashcode() is not always possible, because the class may be const or the proper arguments to initialize are not known at that point in code.
One solution: initialize() only initializes once. If an underlying struct is already initialized with a different value an exception is thrown. That will allow us to initialize a object directly after its construction without fear of a performance penalty.
Initialy, I had problems with using hashcode() because initialize() was not called and made some workarounds to get it working (like copying the input object, and initalizing it with default values). After some thought I realized that my actual issue is not that hashcode() has a pre-requisite (initialize() ) has been called. But rather that it is not very clear when best to invoke inialize() in the first place. And that relates to the fact that when creating class instances from a bitstream is top-down. But creating of class instances is bottom-up (so from child members of a zserio struct to its enclosing parent struct). initialize() is more suited for the first case (because it will also call initialize() on the child objects). When creating objects bottom-up that does not make sense.
Thanks a lot to share ideas. initialize() and initializeChildren() methods are not very intuitive. We should definitelly improve API here to make it clearer.