0

Resharper changed (with my approval, of course) this:

private static Subdepartment GetSubdepartmentForXMLElement(XElement subdept)
{
    return new Subdepartment
    {
        Id = Convert.ToInt32(subdept.Element("Id")),
        AccountId = Convert.ToDouble(subdept.Element("AccountId ")),
        Name = subdept.Element("Name")
    };
}

...into this:

private static Subdepartment GetSubdepartmentForXMLElement(XElement subdept)
{
    var xElement = subdept.Element("Id");
    if (xElement != null)
    {
        var element = subdept.Element("AccountId");
        if (element != null)
        {
            var el = subdept.Element("Name");
            if (el != null)
            {
                return new Subdepartment
                           {
                               Id = Convert.ToInt32(xElement.Value),
                               AccountId = Convert.ToDouble(element.Value),
                               Name = el.Value
                           };
            }
        }
    }
}

...to which I had to add:

return null;

...after the penultimate curly brace, to allow it to compile.

I love R#, but the code above looks 9X uglier than a bag of butts to me; wouldn't this have been a better sharperization:

private static Subdepartment GetSubdepartmentForXMLElement(XElement subdept)
{
    var IdElement = subdept.Element("Id");
    var AccountIdElement = subdept.Element("AccountId");
    var NameElement = subdept.Element("Name");
    if ((IdElement != null) && (AccountIdElement != null) && (NameElement != null))
    {
        return new Subdepartment
        {
            Id = Convert.ToInt32(IdElement.Value),
            AccountId = Convert.ToDouble(AccountIdElement.Value),
            Name = NameElement.Value
        };
    }
    return null;
}

?

UPDATE

I actually prefer this "style" for some reason:

var IdElement = itemGroup.Element("Id");
var item_group_idElement = itemGroup.Element("item_group_id");
var nameElement = itemGroup.Element("name");
if ((null == IdElement) || (null == item_group_idElement) || (null == nameElement)) return null;
return new ItemGroup
{
    Id = Convert.ToInt32(itemGroup.Element("Id").Value),
    item_group_id = itemGroup.Element("item_group_id").Value,
    name = itemGroup.Element("name").Value
};
4

1 回答 1

4

这个:

private static Subdepartment GetSubdepartmentForXMLElement(XElement subdept)
{
    return new Subdepartment
    {
        Id = Convert.ToInt32(xElement.Value),
        AccountId = Convert.ToDouble(element.Value),
        Name = el.Value
    };
}

...甚至不编译。xElement, element, 和el未声明。

但是,resharper 不应该抱怨这一点:

private static Subdepartment GetSubdepartmentForXmlElement(XElement subdept)
{
    if (subdept == null) throw new ArgumentNullException("subdept");

    var idElement = subdept.Element("Id");
    var accountIdElement = subdept.Element("AccountId");
    var nameElement = subdept.Element("Name");

    if (idElement != null && accountIdElement != null && nameElement != null)
    {
        return new Subdepartment
        {
            Id = Convert.ToInt32(idElement.Value),
            AccountId = Convert.ToDouble(accountIdElement.Value),
            Name = nameElement.Value
        };
    }
    return null;
}

Resharper 代码更正一次仅适用于一个问题,这就是您最终在 OP 中遇到嵌套混乱的原因。如果您从一开始就仔细编写代码,您将需要做更少的这些单一问题的修复。

就我个人而言,我更喜欢减少嵌套,并且希望这个更好一点:

private static Subdepartment GetSubdepartmentForXmlElement(XElement subdept)
{
    if (subdept == null) throw new ArgumentNullException("subdept");

    var idElement = subdept.Element("Id");
    var accountIdElement = subdept.Element("AccountId");
    var nameElement = subdept.Element("Name");

    if (idElement == null || accountIdElement == null || nameElement == null)
        return null;

    return new Subdepartment
    {
        Id = Convert.ToInt32(idElement.Value),
        AccountId = Convert.ToDouble(accountIdElement.Value),
        Name = nameElement.Value
    };
}

ReSharper 也不会抱怨这一点。

于 2015-01-09T00:31:02.583 回答