|
|
От: | Дмитрий Наумов | |
| Дата: | 28.09.10 12:35 | ||
| Оценка: | |||
Write trivial constructors
Constructors should never perform any work. Since tests always have to invoke the constructor, an expensive constructor hampers unit testing. Moving expensive code to an init method doesn’t help, because most tests need to work with an initialized object. At the same time, an object should be ready to use as soon as it is constructed. It is awkward when an object requires certain setters to be invoked before it is viable. Clients may resort to guess-and-check programming to find all the methods that need to be invoked in order to make the object usable. The solution is to ask for fully-initialized dependencies in the constructor. For example, instead of connecting to a database in a constructor or an init method, add a constructor parameter for either an open Connection object or preferably for the actual queried data.