Lets say I have an immutable class with a few properties, some of which can be calculated from with in the class, now, I could let the caller calculate the value of each property, or, I could let the class calculate each the properties itself.
For example, in the class below A
must be defined. B
& C
's values can be calculated from A
.
Letting the caller define the properties,
class Z
{
public int A { get; private set; }
public int B { get; private set; }
public int C { get; private set; }
public Z(int a, int b, int c)
{
A = a; // Must always be explicitly defined.
B = b; // Explicit definition is optional.
C = c; // Explicit definition is optional.
}
}
Letting the class define as many properties as possible,
class Z
{
public int A { get; private set; }
public int B { get; private set; }
public int C { get; private set; }
public Z(int a)
{
A = a; // Must always be explicitly defined.
B = // Calculation logic involving A...
C = // More calculation logic involving A...
}
}
So are there any conventions stating when/where to use one method over the other (and why)?