I wouldn't shorten it and i don't like comments (seperate discussion but it usualyl means your code is hard to read)
I would also try to see if you could avoid the daydifference and just pass in number of hours worked.
It would be easier to maintain and probably correspond directly to the law you are obeying.
Anyway for example with the give restrictions and not 100% perfect but you get the point:
public class GiveTheManABreak
{
public enum DaysOff
{
HalfADayOff,
OneDayOff,
NoDaysOff,
}
public DaysOff CalculatesDaysOff(bool typeOfDelegation, int differenceInDays, DateTime arrivalDay)
{
if (typeOfDelegation)
return DelgateCalculateDaysOff(differenceInDays, arrivalDay);
if (BeenHereTooLong(differenceInDays))
return DaysOff.OneDayOff;
if (ArrivedAfterTenAndHasBeenHereZeroOrOneDay(differenceInDays, arrivalDay.TimeOfDay.Hours))
return DaysOff.HalfADayOff;
if (Arrived10pmOrEarlierTwoDaysAgo(differenceInDays, arrivalDay.TimeOfDay.Hours))
return DaysOff.HalfADayOff;
if (ArrivedAfter10pmTwoDaysAgo(differenceInDays, arrivalDay.TimeOfDay.Hours))
return DaysOff.OneDayOff;
return DaysOff.NoDaysOff;
}
private bool BeenHereTooLong(int differenceInDays)
{
return differenceInDays > 2;
}
private bool ArrivedAfter10pmTwoDaysAgo(int differenceInDays, int arrivalTime)
{
if (differenceInDays != 2)
return false;
return arrivalTime > 22;
}
private bool Arrived10pmOrEarlierTwoDaysAgo(int differenceInDays, int arrivalTime)
{
if (differenceInDays != 2)
return false;
return arrivalTime <= 22;
}
private bool ArrivedAfterTenAndHasBeenHereZeroOrOneDay(int differenceInDays, int arrivalTime)
{
if (differenceInDays > 2)
return false;
return arrivalTime > 22;
}
private DaysOff DelgateCalculateDaysOff(int differenceInDays, DateTime arrivalDay)
{
return differenceInDays > 29 ? DaysOff.OneDayOff : DaysOff.NoDaysOff;
}
}
differenceInDays
be negative? Cansosire.TimeOfDay.Hours
be greater than 23? \$\endgroup\$else
's, other cases just modify the (member? local?) variabledays
. Please describe whetherdays
is a local variable, and what is returned in case of e.g. tipDelegatie==true and differenceInDays > 2. I can assume that at the anddays
value is a result of this method, but it is not reflected in your code \$\endgroup\$differenceInDays == 2 && sosire.TimeOfDay.Hours == 22
you hit the finalelse
? \$\endgroup\$