English 中文(简体)
采用太多的ool制方法
原标题:Refactoring a method with too many bool in it

I have this method in c#, and I wish to refactor it. There are just too many bools and lines. What would be the best refactoring. Making a new class seems a bit overkill, and cutting simply in two seems hard. Any insight or pointer would be appreciated.

B. 计算方法

    private DialogResult CheckForSireRestrictionInSubGroup(bool deletingGroup,string currentId)
    {
        DialogResult result = DialogResult.No;
        if (!searchAllSireList)
        {
            DataAccessDialog dlg = BeginWaitMessage();
            bool isClose = false;
            try
            {
                ArrayList deletedSire = new ArrayList();
                ISireGroupBE sireGroupBE = sireController.FindSireGroupSearch();

                if (sireGroupBE != null)
                {
                    //if the current group is in fact the seach group before saving
                    bool currentGroupIsSeachGroup = sireGroupBE.TheSireGroup.id == currentId; 

                    //if we have setting this group as search group
                    bool selectedAsSearchGroup = this.chkBoxSelectedSireGroup.Checked;

                    //if the group we currently are in is not longer the seach group(chk box was unchecked)
                    bool wasSearchGroup = currentGroupIsSeachGroup && !selectedAsSearchGroup;

                    //if the group is becoming the search group
                    bool becomesSearchGroup = !currentGroupIsSeachGroup && selectedAsSearchGroup;

                    //if the group being deleted is in fact the search group
                    bool deletingSearchGroup = deletingGroup && currentGroupIsSeachGroup;

                    //if the user checked the checkbox but he s deleting it, not a so common case, but
                    //we shouldn t even consider to delete sire in this case
                    bool deletingTemporarySearchGroup = deletingGroup && !currentGroupIsSeachGroup;         

                    //if we are not deleting a temporary search group and it s either
                    //becoming one (without deleting it) or we already are the search group
                    bool canDeleteSires = !deletingTemporarySearchGroup && 
                                          (becomesSearchGroup || currentGroupIsSeachGroup);
                    //we only delete sires if we are in search group
                    if (canDeleteSires)
                    {   
                        if (deletingSearchGroup || wasSearchGroup)
                        {
                            // If we deleted all sires
                            deletedSire = new ArrayList();
                            deletedSire.AddRange( sireGroupBE.SireList);
                        }
                        else
                        {
                            //if we delete a few sire from the change of search group
                            deletedSire = GetDeleteSire(sireGroupBE.SireList);
                        }
                    }

                    EndWaitMessage(dlg);
                    isClose = true;
                    result =  ShowSubGroupAffected(deletedSire);
                }
            }
            finally
            {
                if (!isClose)
                {
                    EndWaitMessage(dlg);
                }
            }
        }

        return result;
    }
最佳回答

一种选择是,将每一首批小船重新排列(canDeleteSires,deletingSearchGroup able )为描述逻辑的读写法:

if (WeAreInSearchGroup())
{
    if (WeAreDeletingAllSires())
    {
        deletedSire = new ArrayList();
        deletedSire.AddRange( sireGroupBE.SireList);
    }
    else
    {
        deletedSire = GetDeleteSire(sireGroupBE.SireList);
    }
}

然后,你将你目前的ool风逻辑推到这些方法中,你如何通过国家(方法论或阶级成员)是一个tas。

这将把主要方法中的小船人移至直接提出和回答问题的较小方法。 我看到,在发展的“成果是邪恶”模式中采用这种做法。 坦率地说,我认为,如果你是一夫一妻,那就没有什么超高技能,但是在一个团队中,可以读得非常容易。

鉴于个人的偏好,如果提前发言,我也首先要推翻你,这将降低整个方法的迟缓程度:

if (searchAllSireList)
{
    return result;
}

DataAccessDialog dlg = BeginWaitMessage();
bool isClose = false;
try
...

但是,你们可能受到“多重回报是邪恶”人群的困扰。 我的印象是,发展实践与政治一样。

问题回答

这对于消除某些tation呆症来说是一个小的佐证:

private DialogResult CheckForSireRestrictionInSubGroup(bool deletingGroup,string currentId)
{
    if (searchAllSireList)
        return DialogResult.No;

    DataAccessDialog dlg = BeginWaitMessage();
    bool isClose = false;

    try
    {
        ISireGroupBE sireGroupBE = sireController.FindSireGroupSearch();

        if (sireGroupBE == null)
            return DialogResult.No;

        //if the current group is in fact the seach group before saving
        bool currentGroupIsSeachGroup = sireGroupBE.TheSireGroup.id == currentId; 

        //if we have setting this group as search group
        bool selectedAsSearchGroup = this.chkBoxSelectedSireGroup.Checked;

        //if the group we currently are in is not longer the seach group(chk box was unchecked)
        bool wasSearchGroup = currentGroupIsSeachGroup && !selectedAsSearchGroup;

        //if the group is becoming the search group
        bool becomesSearchGroup = !currentGroupIsSeachGroup && selectedAsSearchGroup;

        //if the group being deleted is in fact the search group
        bool deletingSearchGroup = deletingGroup && currentGroupIsSeachGroup;

        //if the user checked the checkbox but he s deleting it, not a so common case, but
        //we shouldn t even consider to delete sire in this case
        bool deletingTemporarySearchGroup = deletingGroup && !currentGroupIsSeachGroup;         

        //if we are not deleting a temporary search group and it s either
        //becoming one (without deleting it) or we already are the search group
        bool canDeleteSires = !deletingTemporarySearchGroup && 
                              (becomesSearchGroup || currentGroupIsSeachGroup);

        ArrayList deletedSire = new ArrayList();

        //we only delete sires if we are in search group
        if (canDeleteSires)
        {   
            if (deletingSearchGroup || wasSearchGroup)
            {
                // If we deleted all sires
                deletedSire.AddRange(sireGroupBE.SireList);
            }
            else
            {
                //if we delete a few sire from the change of search group
                deletedSire = GetDeleteSire(sireGroupBE.SireList);
            }
        }

        EndWaitMessage(dlg);
        isClose = true;
        return ShowSubGroupAffected(deletedSire);
    }
    finally
    {
        if (!isClose)
        {
            EndWaitMessage(dlg);
        }
    }
    return DialogResult.No;
}

实际上,我个人会离开。 你全身拥有的机器人虽然效率不高,但使得这一功能可以阅读,容易理解。

你们可以做一些事情,如把所有包裹放在一线上(如下文),但这不能像你所写的那样维持。

x = (a & b) & ! d) 。

或许你可以尝试删除所有意见。 你在增加法典理解的价值时,可以把其中几个变量放在网上,但我认为这将增加任何价值。

另一方面,这部法典以你的形式出现,因此在你的控制人员中可能更好,这样你就可以保持形式简单,控制者实际上控制行为。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...