I heard it a lot of times. When I was learning Java, a flash definition about constructor methods faded away in the classroom. A “silly” definition that nobody seemed to remember a bunch of years later.
Sometimes the things that are easily forgotten are the most basic principles. We take them as a fact and we rarely reevaluate them.
One of the basics in OOP is the role of a constructor method:
A constructor method is a special method that contains ONLY the required initializations for the instance to be created in a valid state, ready to be used.
Said this, any constructor what invokes complex methods, services to retrieve data, contains lots of lines of code, or even loops, IS A MISTAKE and needs to be fixed.
A constructor has to be minimum! The instance should be created just in time and any complex initialization should be the responsibility of any other method of the instance.
This answer on Stack Overflow is very good. Be sure to read it 🙂 and keep you ctors clean!
Number of attibutes in a class
Anything more than 10 attributes is starting to smell.
“No shit is accepted.”
Courtesy of Javier Peris Morillo.
It’s not so simple to just make a class implement the INotifyPropertyChanged and create some properties.
Normally you have to REPRESENT a model. But how? Replicating the properties in the model that you want? Exposing the model itself using a property?
That is a good question that every MVVM lover should have asked himself when writhing a ViewModel.
You can take a look at this article. It really hits the nail on the head.
I normally expose the model directly and make UI classes that implement INotifyPropertyChanged. But I see the property mapping far more elegant and pure MVVMish 😉
What do you prefer?
Don’t leave it unread!