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
};