Is it bad practice to have a long initialization method?
Posted
by Paperflyer
on Stack Overflow
See other posts from Stack Overflow
or by Paperflyer
Published on 2010-04-12T13:20:16Z
Indexed on
2010/04/12
13:23 UTC
Read the original article
Hit count: 261
many people have argued about function size. They say that functions in general should be pretty short. Opinions vary from something like 15 lines to "about one screen", which today is probably about 40-80 lines.
Also, functions should always fulfill one task only.
However, there is one kind of function that frequently fails in both criteria in my code: initialization functions.
For example in an audio application, the audio hardware/API has to be set up, audio data has to be converted to a suitable format and the object state has to properly initialized. These are clearly three different tasks and depending on the API this can easily span more than 50 lines.
The thing with init-functions is that they are generally only called once, so there is no need to re-use any of the components. Would you still break them up into several smaller functions would you consider big initialization functions to be ok?
© Stack Overflow or respective owner