myAlgo.setInputs(inputStruct);The correct way is to refactor calculate() method as follows:
myAlgo.calculate();
myAlgo.getOutputs(outputStruct);
outputStruct out = calculate(inputStruct)Using this version would save the user of myAlgo from a couple of lines, he would not face the risk of forgetting to set inputs. In the previous version, if you forget to call setInputs(), the compiler will happily build your code and you will waste time finding the bug at run time. In the new version, if you forget to pass inputs to calculate(), it won't build and you will instantly see the bug.
Whenever you have multiple public initialization functions, try to combine them into a constructor. Your design should be such that when your code builds successfully, you should be confident that it has no initialization or finalizations related bugs. Let the compiler help you.
No comments:
Post a Comment