设置条件时的逻辑问题

时间:2010-02-04 10:02:20

标签: c#

我收到了这样的评论

此检查(if (rp != null))不应该在这里。当rp为irp.ResourcePolicy时,变量null应设置为null,因为资源策略未知。否则,将使用先前的值,并且之前的值可能不正确。

// Search for the specified resource policy
                                ResourcePolicy rp = null;
                                try
                                {
                                    int rpindex = allObjects.Find(new Guid(policyGuid));
                                    if (rpindex != -1)
                                    {
                                        rp = (ResourcePolicy)allObjects.GetAt(rpindex);
                                    }
                                }
                                catch (System.Exception err)
                                {
                                    SpoDebug.DebugTraceSevere(func, "Bad GUID: " + policyGuid + "  Exception: " + err.Message);
                                    rp = null;
                                }

                                if (rp == null)
                                {
                                    SpoDebug.DebugTraceSevere(func, "Unable to find ResourcePolicy with GUID: " + policyGuid);
                                }

                                // Search for the specified host   


foreach (DataModelObject dmo in allObjects)
{
    if (dmo is IResourcePolicy && string.Compare(dmo.Name, hostName, true) == 0)
    {
        IResourcePolicy irp = (IResourcePolicy)dmo;
        irp.AgentVersion = agentVersion;
        if (rp != null)  // this is the condition we need to look
        {
            irp.ResourcePolicy = rp;
            irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
            irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
        }
        // Distribute the object without saving it.
        SpoServer.Spurt.ServerSendObject(dmo, true, 0);

        break;
   }

}

所以我这样做了:

foreach (DataModelObject dmo in allObjects)
{
    if (dmo is IResourcePolicy && string.Compare(dmo.Name, hostName, true) == 0)
    {
        IResourcePolicy irp = (IResourcePolicy)dmo;
        irp.AgentVersion = agentVersion;
        if (rp == null)
        {
            irp.ResourcePolicy = null;
        }
        else
        {
            irp.ResourcePolicy = rp;
            irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
            irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
        }

        // Distribute the object without saving it.
        SpoServer.Spurt.ServerSendObject(dmo, true, 0);

        break;
    }
}

这还够吗?

2 个答案:

答案 0 :(得分:1)

对我来说没问题 - 尽管irp.ResourcePolicy = rp;在所有情况下都是正确的,所以你可能会把它移出条件块(虽然把它放在一个块中你会让它变得更明显随便读者)。

答案 1 :(得分:1)

irp.AgentPolicyVersion.Version为空时,您希望irp.ResourcePolicyEnabledrp是什么?我希望它们被设置为一些适当的默认值,而不是保持原样。

否则,我会这样写:

IResourcePolicy irp = (IResourcePolicy)dmo;
irp.AgentVersion = agentVersion;
irp.ResourcePolicy = rp;
if (rp != null)
{
    irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
    irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
}