C3b0b8da886396ca8d5a28ad397c404d

Our reporting program uses these functions to merge all the similar timesheet entries together for our employees. I'm thinking there has to be something that can be improved upon here.

private static HumanResource CompressTimesheetItems(HumanResource employee)
{
	List<TimesheetItemObject> CompressedTimesheetItems = new List<TimesheetItemObject>();

	for (int i = 0; i < employee.Timesheet.TimesheetLines.Count; i++)
	{
		TimesheetItemObject item = employee.Timesheet.TimesheetLines[i];

		bool HadMatch = false;
		foreach (TimesheetItemObject CompressedItem in CompressedTimesheetItems)
		{
			if (CanMerge(CompressedItem, item))
			{
				CompressedItem.RegularTime  += item.RegularTime;
				CompressedItem.OvertimeTime += item.OvertimeTime;
				HadMatch = true;

				break;
			}
		}

		if (HadMatch == false)
		{
                        // Create a copy with the fine detail descriptions
			TimesheetItemObject CompressedItem = item.Clone();
			CompressedItem.ProjectDescription = "";
			CompressedItem.TaskDescription = "";

			CompressedTimesheetItems.Add(CompressedItem);
		}
	}

	employee.Timesheet.TimesheetLines = CompressedTimesheetItems;
	return employee;
}

private static bool CanMerge(TimesheetItemObject source, TimesheetItemObject target)
{
	if (
		(source.Timestamp.ToShortDateString()   == target.Timestamp.ToShortDateString()) &&
		(source.JobNumber                       == target.JobNumber) &&
		(source.OpCode                          == target.OpCode) &&
		(source.PayCode                         == target.PayCode)
	   )
	{
		return true;
	}

	return false;
}

Refactorings

No refactoring yet !

F9a9ba6663645458aa8630157ed5e71e

Ants

August 8, 2010, August 08, 2010 06:36, permalink

2 ratings. Login to rate!

I would have made something that just focused on the timesheet items collection.

class TimesheetItemCompressor : IEqualityComparer<TimesheetItemObject>
{
    public bool Equals(TimesheetItemObject x, TimesheetItemObject y)
    {
        return x.Timestamp.Date == y.Timestamp.Date
            && x.JobNumber == y.JobNumber
            && x.OpCode == y.OpCode
            && x.PayCode == y.PayCode;
    }

    public int GetHashCode(TimesheetItemObject obj)
    {
        int hash = obj.Timestamp.Date.GetHashCode();
        hash = (hash * 7) + obj.JobNumber.GetHashCode();
        hash = (hash * 7) + obj.OpCode.GetHashCode();
        hash = (hash * 7) + obj.PayCode.GetHashCode();
        return hash;
    }

    TimesheetItemObject Merge(TimesheetItemObject key, IEnumerable<TimesheetItemObject> group)
    {
        // We could have used:
        //      RegularTime = new TimeSpan(group.Sum(line => line.RegularTime.Ticks)),
        //      OvertimeTime = new TimeSpan(group.Sum(line => line.OvertimeTime.Ticks)),
        // and put everything in the Compress() method, but that would iterate
        // over the group twice.

        var line = new TimesheetItemObject()
        {
            Timestamp = key.Timestamp,
            JobNumber = key.JobNumber,
            OpCode = key.OpCode,
            PayCode = key.PayCode,
            ProjectDescription = "",
            TaskDescription = "",
        };
        foreach (var item in group)
        {
            line.RegularTime += item.RegularTime;
            line.OvertimeTime += item.OvertimeTime;
        }
        return line;
    }

    public IList<TimesheetItemObject> Compress(IEnumerable<TimesheetItemObject> lines)
    {
        return lines.AsQueryable()
                    .GroupBy(line => line, (key, group) => Merge(key, group), this)
                    .ToList();
    }
}
1c9ff70524592d0805935cc81fd6b817

Pharmacy technician book

August 8, 2010, August 08, 2010 21:08, permalink

No rating. Login to rate!

Great information! I’ve been looking for something like this for a while now. Thanks!

Your refactoring





Format Copy from initial code

or Cancel