Sometimes when you see too much code in constructor, the first question comes to your mind is "How much and how many lines and what all???"
In old compilers before .net old C++ compiler it was a clear NO NO.
These days its debatable, few say constructor is nothing but a function and you can use it as any other functions in the class.
Question - if this was the case then why a constructor was needed?
Answer - (From someone who likes writing too much code) - Its a function that get called automatically when creating instance. (good answer)
Question - What about (SOLID) open-close principle?
Answer - Its not open for extension, you extend it, you are forced to incorporate unwanted functionality in ctor.
Question - If the functionality fails, how would you segregate, report and log the error or fault or info?
Answer - Not any good answer.
Question - You follow SOLID and you are using dependency-invesion, how would you debug the problem as its already convoluted to debug object creation in any dependency framework.
Answer - Not any good answer.
Question- What about unit testing, would like you execute bunch of functionality everytime when you create new instance?
Question - Not any good answer.
So what should be an ideal constructor be doing?
Nothing but initialising the fields, following SOLID and passing dependencies as parameters and assigning them to fields.
Feel free to comment.
In old compilers before .net old C++ compiler it was a clear NO NO.
These days its debatable, few say constructor is nothing but a function and you can use it as any other functions in the class.
Question - if this was the case then why a constructor was needed?
Answer - (From someone who likes writing too much code) - Its a function that get called automatically when creating instance. (good answer)
Question - What about (SOLID) open-close principle?
Answer - Its not open for extension, you extend it, you are forced to incorporate unwanted functionality in ctor.
Question - If the functionality fails, how would you segregate, report and log the error or fault or info?
Answer - Not any good answer.
Question - You follow SOLID and you are using dependency-invesion, how would you debug the problem as its already convoluted to debug object creation in any dependency framework.
Answer - Not any good answer.
Question- What about unit testing, would like you execute bunch of functionality everytime when you create new instance?
Question - Not any good answer.
So what should be an ideal constructor be doing?
Nothing but initialising the fields, following SOLID and passing dependencies as parameters and assigning them to fields.
Feel free to comment.