In C++ doing heavy lifting in the constructor is discouraged for, amongst other things, if an exception is thrown after memory allocation a memory leak may be created. In Java it is still discouraged but it holds less importance due to the garbage collector. In objective C where is the stance on the init method with regard to heavy lifting?
In C++ doing heavy lifting in the constructor is discouraged for, amongst other things,
Share
The general guidance is that lazy-load is preferred for anything that’s expensive. Generally speaking,
initshould avoid expensive calls since you might not need the results. Maybe the caller creates this object and then throws it away, or only looks at one value. You’d like to avoid creating massive data structures that aren’t needed. This guidance is to improve performance; it is not a hard-and-fast rule.It’s acceptable, but uncommon, to allow
initto fail and returnnil. See bbum’s answer here for the correct approach: Returning nil in the init in Objective C code.