I am trying to implement some of the principles laid out in Clean Code by Robert C. Martin.
I had a class that was heavily suffering from the ordering problem. I have solved most of this by extracting the code into separate classes.
I am now left with my main class looking like this:
public class Planner {
public EstimateCollection ChosenEstimates { get; set; } // Want a better name for this..
public PlannedYear YearPlan { get; set; }
public Planner(List<Estimate> estimates) {
ChosenEstimates = new EstimateCollection(estimates);
YearPlan = ReadExistingPlan();
}
private PlannedYear ReadExistingPlan() {
var planReader = new PlanReader();
var rawPlan = planReader.GetScheduleFromDatabase(ChosenEstimates.StartDate, ChosenEstimates.EndDate);
var planConverter = new PlanConverter();
return planConverter.ConvertRowsToPlannedYear(rawPlan);
}
}
// This class was made by extracting most of what used to be in Planner's constructor
public class EstimateCollection {
public List<Estimate> Estimates { get; set; }
public DateTime StartDate { get; }
public DateTime EndDate { get; }
public EstimateCollection(List<Estimate> estimates) {
Estimates = estimates;
StartDate = GetEarliestStartDate();
EndDate = GetLatestEndDate();
}
private DateTime GetEarliestStartDate() {
// Do something to get start date from Estimates list
}
private DateTime GetLatestEndDate() {
// Do something to get end date Estimates list
}
}
The problem I have now is on in the constructor (line #8) in the Planner class. First the ChosenEstimates
variable is set, then the YearPlan
variable is set. The ChosenEstimates
var must be set before YearPlan
can be set via the ReadExistingPlan()
method because ReadExistingPlan()
uses ChosenEstimates
.
I could supply ChosenEstimates
to ReadExistingPlan()
as a parameter, but the Clean Code examples almost never do this.
Are there further structural changes to my code that can avoid this?
Right now I am choosing which problem I would rather have.