In a legacy Java project, when adding a new feature - an existing email field can be editable or not editable on base of its parent system, a developer added a new feature by adding a new class, which is added as an instance field to the existing class containing thousands of lines because the logic of deciding the editability of the field is not trivial.
However, one code reviewer commented that all fields and operation of logical entity should be represented by single class.
Question: should all fields and operation of logical entity be represented by single class even though that single class has already thousands of lines
EmailField
, which has only one methodisEditable()
currently, i.e. should the email field be editable or not, and this new class indeed has dependency on the existing lengthy class (assume the name isA
), meaningEmailField
has a constructorEmailField(A parent)
and theisEditable()
method is not of a trivial one line of code, then in the existing class, theEmailField
is initialized asnew EmailField(this)
EmailField
sounds like it could be a good abstraction. However, the cyclic dependency is something one should consider to get rid off, especially if the existing class "A" offers a broad public interface, whilstEmailField
only uses a small part of that interface. Maybe the constructor ofEmailField
could have a signature with some explicit parameters, maybe some of them just delegates/callbacks? That would it make probably simpler to write a unit test forEmailField
, to test it in isolation apart from the classA
.