Getting the first line frame count I'm trying to improve my functional skills and seeing where it fits and where it might not. Please review the code and see where functional practices might be applied. I'm specifically looking at trying to get rid of the state variables.

private int GetFirstLineFrameCount(XDocument doc)
{
    var subElement = (from res in doc.Descendants(_xmlns + "subelements")
                      where res.Element(_xmlns + "object")
                                 .Element(_xmlns + "metadata")
                                 .Element(_xmlns + "identifier").Value == "1"
                      select res).FirstOrDefault();

    var offsetElement = subElement.Descendants(_xmlnsEc + "start").Select(x =&gt; x.Element(_xmlnsEc + "editUnitNumber")).FirstOrDefault();

    if (offsetElement == null)
    {
        Logger.Error&lt;SubtitleNormalizer&gt;("no offset found");
        return 0;
    }

    return Int32.Parse(offsetElement.Value);
}
A refactored functional programming that is evil.

public static class ObjectExtension
{
    public static Tout IfNotNull&lt;Tin, Tout&gt;(this Tin source, Func&lt;Tin, Tout&gt; func)
        where Tout : class
        where Tin : class
    {
        if (source == null)
        {
            return null;
        }
        else
        {
            return func(source);
        }
    }
}

public static class stringExtension
{
    public static int ToIntDefault(this string str, Func&lt;int&gt; def)
    {
        if (!string.IsNullOrEmpty(str))
        {
            return int.Parse(str);
        }
        return def();
    }
}


Consumer:

private int GetFirstLineFrameCount(XDocument doc, string _xmlns, string _xmlnsEc)
{
    int result = 0;
    var subElement = (from res in doc.Descendants(_xmlns + "subelements")
                      where res.Element(_xmlns + "object")
                                 .Element(_xmlns + "metadata")
                                 .Element(_xmlns + "identifier").Value == "1"
                      select res)
                      .FirstOrDefault()
                      .IfNotNull(k =&gt;
                          k.Descendants(_xmlnsEc + "start").Select(x =&gt; x.Element(_xmlnsEc + "editUnitNumber")).FirstOrDefault()
                      )
                      .IfNotNull(k =&gt;
                          {
                              result = k.Value.ToIntDefault(
                                  () =&gt; {
                                          Logger.Error&lt;SubtitleNormalizer&gt;("no offset found");
                                          return 0;
                                      }
                                  );
                              return (object)null;
                          }
                      );
    return result;
}


No, don't follow this. It is evil.

I just demonstrate something that can be done with functions. What you can do is as dreza's suggest to accept _xmlns and _xmlnsEc as parameter. Otherwise, I found that both of the variable is legal to exists, just make them readonly and do constructor injection. Otherwise, create a configuration for both variable so you can use myXmlConfiguration.Xmlns and myXmlConfiguration.XmlnsEc.