Avatar

I have these two very similar methods. Both do the same thing, but one is based off the enum FIRSTSTATUS and the other is based off SECONDSTATUS. I'd like to cut this down to one method, but I'm not sure the best way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
private static bool SetFolderBasedOnFirstStatus(PurchaseOrder po)
{
    bool documentFinished = false;

    if (po.FirstStatus == FIRSTSTATUS.Complete)
    {
        po.Folder = DOCUMENTFOLDER.History;
        documentFinished = true;
    }
    else if (po.FirstStatus == FIRSTSTATUS.Partial)
        po.Folder = DOCUMENTFOLDER.InProgress;
    else
    {
        po.Folder = DOCUMENTFOLDER.Inbox;    
    }
    return documentFinished;
}

private static bool SetFolderBasedOnSecondStatus(PurchaseOrder po)
{
    bool documentFinished = false;

    if (po.SecondStatus == SECONDSTATUS.Complete)
    {
        po.Folder = DOCUMENTFOLDER.History;
        documentFinished = true;
    }
    else if (po.SecondStatus == SECONDSTATUS.Partial)
    {
        po.Folder = DOCUMENTFOLDER.InProgress;
    }

    return documentFinished;
}

Refactorings

No refactoring yet !

F9a9ba6663645458aa8630157ed5e71e

Ants

January 25, 2010, January 25, 2010 04:11, permalink

1 rating. Login to rate!

Give this a try...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    static bool SetFolderBasedOnStatus<T>(PurchaseOrder po, T value, T completeValue, T partialValue)
        where T : IComparable
    {
        if (value.CompareTo(completeValue) == 0)
            po.Folder = DOCUMENTFOLDER.History;
        else if (value.CompareTo(partialValue) == 0)
            po.Folder = DOCUMENTFOLDER.InProgress;
        return po.Folder == DOCUMENTFOLDER.History;
    }

    private static bool SetFolderBasedOnFirstStatus(PurchaseOrder po)
    {
        po.Folder = DOCUMENTFOLDER.Inbox;
        return SetFolderBasedOnStatus(po, po.FirstStatus, FIRSTSTATUS.Complete, FIRSTSTATUS.Partial);
    }

    private static bool SetFolderBasedOnSecondStatus(PurchaseOrder po)
    {
        return SetFolderBasedOnStatus(po, po.SecondStatus, SECONDSTATUS.Complete, SECONDSTATUS.Partial);
    }

Your refactoring





Format Copy from initial code

or Cancel