Complex Member

CodeRush shows the Complex Member code issue if a member maintenance complexity value is equal to or higher then 800.

Fix

Break down the current method into smaller methods.

Purpose

The Complex Member code issue draws your attention to the most complex members, because they are difficult to read and understand.

Example

//Maintenance complexity: 964
public void ConvertUserDataFromTextToXml(string[] searchDirs, string outputDirectory, SaveMode saveMode)
{
    if (searchDirs == null || searchDirs.Length == 0)
        return;
    List<string> files = new List<string>();
    //Searching .txt files
    foreach (string directory in searchDirs)
        files.AddRange(Directory.GetFiles(directory, "*.txt"));
    foreach (string txtPath in files)
    {
        if (!File.Exists(txtPath))
            continue;
        List<UserInfo> Users = new List<UserInfo>();
        string[] lineElements;
        int lineNum = 0;
        UserInfo user;
        ExperienceLevel experience;
        using (StreamReader sReader = File.OpenText(txtPath))
        {
            //Creating the list of users
            while (!sReader.EndOfStream)
            {
                lineNum++;
                lineElements = sReader.ReadLine().Split(',');
                if (lineElements.Length < 4)
                {
                    Console.WriteLine(string.Format("Line {0} not complete", lineNum));
                    continue;
                }
                switch (lineElements[3])
                {
                    case "experienced":
                        experience = ExperienceLevel.Experienced;
                        break;
                    case "expert":
                        experience = ExperienceLevel.Expert;
                        break;
                    default:
                        experience = ExperienceLevel.New;
                        break;
                }
                user = new UserInfo(lineElements[0], lineElements[1], DateTime.Parse(lineElements[2]), experience);
                Users.Add(user);
            }
        }
        if (Users.Count == 0)
        {
            Console.WriteLine("No user info found");
            return;
        }
        Console.WriteLine(string.Format("{0} users found", Users.Count));
        XmlSerializer xSerializer;
        XmlWriterSettings xs;
        XmlWriter xWriter;
        string xmlName;
        string xmlFullName;
        int index;
        if (!Directory.Exists(outputDirectory))
            Directory.CreateDirectory(outputDirectory);
        switch (saveMode)
        {
            //Saving data to different files depending on experience level of each user
            case SaveMode.ByExperience:
                List<UserInfo> experts = new List<UserInfo>();
                List<UserInfo> newUsers = new List<UserInfo>();
                List<UserInfo> experiencedUsers = new List<UserInfo>();
                foreach (UserInfo uInfo in Users)
                {
                    switch (uInfo.Experience)
                    {
                        case ExperienceLevel.Expert:
                            experts.Add(uInfo);
                            break;
                        case ExperienceLevel.Experienced:
                            experiencedUsers.Add(uInfo);
                            break;
                        case ExperienceLevel.New:
                            newUsers.Add(uInfo);
                            break;
                    }
                }
                //saving new users data
                index = 0;
                xmlName = "newUsers";
                xmlFullName = string.Format("{0}\\{1}.xml", outputDirectory, xmlName);
                while (File.Exists(xmlFullName))
                {
                    index++;
                    xmlFullName = string.Format("{0}\\{1}_{2}.xml", outputDirectory, xmlName, index);
                }
                xSerializer = new XmlSerializer(typeof(List<UserInfo>));
                xs = new XmlWriterSettings(){ Indent = true };
                xWriter = XmlWriter.Create(xmlFullName, xs);
                try
                {
                    xSerializer.Serialize(xWriter, newUsers);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("Serialization failed: {0}", ex.Message));
                }
                xWriter.Close();
                //saving experienced users data                            
                index = 0;
                xmlName = "experiencedUsers";
                xmlFullName = string.Format("{0}\\{1}.xml", outputDirectory, xmlName);
                while (File.Exists(xmlFullName))
                {
                    index++;
                    xmlFullName = string.Format("{0}\\{1}_{2}.xml", outputDirectory, xmlName, index);
                }
                xSerializer = new XmlSerializer(typeof(List<UserInfo>));
                xs = new XmlWriterSettings(){ Indent = true };
                xWriter = XmlWriter.Create(xmlFullName, xs);
                try
                {
                    xSerializer.Serialize(xWriter, experiencedUsers);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("Serialization failed: {0}", ex.Message));
                }
                xWriter.Close();
                //saving experts data
                index = 0;
                xmlName = "experts";
                xmlFullName = string.Format("{0}\\{1}.xml", outputDirectory, xmlName);
                while (File.Exists(xmlFullName))
                {
                    index++;
                    xmlFullName = string.Format("{0}\\{1}_{2}.xml", outputDirectory, xmlName, index);
                }
                xSerializer = new XmlSerializer(typeof(List<UserInfo>));
                xs = new XmlWriterSettings(){ Indent = true };
                xWriter = XmlWriter.Create(xmlFullName, xs);
                try
                {
                    xSerializer.Serialize(xWriter, experts);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("Serialization failed: {0}", ex.Message));
                }
                xWriter.Close();
                break;
            //Saving all users data to a single file
            case SaveMode.SingleFile:
                index = 0;
                xmlName = "Users";
                xmlFullName = string.Format("{0}\\{1}.xml", outputDirectory, xmlName);
                while (File.Exists(xmlFullName))
                {
                    index++;
                    xmlFullName = string.Format("{0}\\{1}_{2}.xml", outputDirectory, xmlName, index);
                }
                xSerializer = new XmlSerializer(typeof(List<UserInfo>));
                xs = new XmlWriterSettings(){ Indent = true };
                xWriter = XmlWriter.Create(xmlFullName, xs);
                try
                {
                    xSerializer.Serialize(xWriter, Users);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("Serialization failed: {0}", ex.Message));
                }
                xWriter.Close();
                break;
            //Saving data of each user to a separate file
            case SaveMode.SpearateFiles:
                foreach (UserInfo uInfo in Users)
                {
                    index = 0;
                    xmlName = string.Format("{0}_{1}", uInfo.Name, uInfo.FamilyName);
                    xmlFullName = string.Format("{0}\\{1}.xml", outputDirectory, xmlName);
                    while (File.Exists(xmlFullName))
                    {
                        index++;
                        xmlFullName = string.Format("{0}\\{1}_{2}.xml", outputDirectory, xmlName, index);
                    }
                    xSerializer = new XmlSerializer(typeof(UserInfo));
                    xs = new XmlWriterSettings(){ Indent = true };
                    xWriter = XmlWriter.Create(xmlFullName, xs);
                    try
                    {
                        xSerializer.Serialize(xWriter, Users);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(string.Format("Serialization failed: {0}", ex.Message));
                    }
                    xWriter.Close();
                }
                break;
        }
    }
}

Fix:

//Maintenance complexity: 41
private List<string> GetFiles(string[] searchDirs)
{
    List<string> files = new List<string>();
    //Searching .txt files
    foreach (string directory in searchDirs)
        files.AddRange(Directory.GetFiles(directory, "*.txt"));
    return files;
}

//Maintenance complexity: 162
private List<UserInfo> GetUsers(string txtPath)
{
    List<UserInfo> Users = new List<UserInfo>();
    string[] lineElements;
    int lineNum = 0;
    UserInfo user;
    ExperienceLevel experience;
    using (StreamReader sReader = File.OpenText(txtPath))
    {
        //Creating the list of users
        while (!sReader.EndOfStream)
        {
            lineNum++;
            lineElements = sReader.ReadLine().Split(',');
            if (lineElements.Length < 4)
            {
                Console.WriteLine(string.Format("Line {0} not complete", lineNum));
                continue;
            }
            switch (lineElements[3])
            {
                case "experienced":
                    experience = ExperienceLevel.Experienced;
                    break;
                case "expert":
                    experience = ExperienceLevel.Expert;
                    break;
                default:
                    experience = ExperienceLevel.New;
                    break;
            }
            user = new UserInfo(lineElements[0], lineElements[1], DateTime.Parse(lineElements[2]), experience);
            Users.Add(user);
        }
    }
    return Users;
}

//Maintenance complexity: 144
private void SaveDataToXml(object data, string outputDirectory, string xmlName)
{
    XmlSerializer xSerializer;
    XmlWriterSettings xs;
    XmlWriter xWriter;
    string xmlFullName;
    int index = 0;
    if (!Directory.Exists(outputDirectory))
        Directory.CreateDirectory(outputDirectory);
    xmlFullName = string.Format("{0}\\{1}.xml", outputDirectory, xmlName);
    while (File.Exists(xmlFullName))
    {
        index++;
        xmlFullName = string.Format("{0}\\{1}_{2}.xml", outputDirectory, xmlName, index);
    }
    xSerializer = new XmlSerializer(data.GetType());
    xs = new XmlWriterSettings() { Indent = true };
    xWriter = XmlWriter.Create(xmlFullName, xs);
    try
    {
        xSerializer.Serialize(xWriter, data);
    }
    catch (Exception ex)
    {
        Console.WriteLine(string.Format("Serialization failed: {0}", ex.Message));
    }
    xWriter.Close();
}

//Maintenance complexity: 268
public void ConvertUserDataFromTextToXml(string[] searchDirs, string outputDirectory, SaveMode saveMode)
{
    if (searchDirs == null || searchDirs.Length == 0)
        return;
    List<string> files = GetFiles(searchDirs);
    foreach (string txtPath in files)
    {
        if (!File.Exists(txtPath))
            continue;
        List<UserInfo> users = GetUsers(txtPath);
        if (users.Count == 0)
        {
            Console.WriteLine("No user info found");
            return;
        }
        Console.WriteLine(string.Format("{0} users found", users.Count));
        switch (saveMode)
        {
            //Saving data to different files depending on experience level of each user
            case SaveMode.ByExperience:
                List<UserInfo> experts = new List<UserInfo>();
                List<UserInfo> newUsers = new List<UserInfo>();
                List<UserInfo> experiencedUsers = new List<UserInfo>();
                foreach (UserInfo uInfo in users)
                {
                    switch (uInfo.Experience)
                    {
                        case ExperienceLevel.Expert:
                            experts.Add(uInfo);
                            break;
                        case ExperienceLevel.Experienced:
                            experiencedUsers.Add(uInfo);
                            break;
                        case ExperienceLevel.New:
                            newUsers.Add(uInfo);
                            break;
                    }
                }
                //saving new users data
                SaveDataToXml(newUsers, outputDirectory, "newUsers");
                //saving experienced users data     
                SaveDataToXml(experiencedUsers, outputDirectory, "experiencedUsers");
                //saving experts data
                SaveDataToXml(experts, outputDirectory, "experts");
                break;
            //Saving all users data to a single file
            case SaveMode.SingleFile:
                SaveDataToXml(users, outputDirectory, "Users");
                break;
            //Saving data of each user to a separate file
            case SaveMode.SpearateFiles:
                foreach (UserInfo uInfo in users)
                    SaveDataToXml(users, outputDirectory, string.Format("{0}_{1}", uInfo.Name, uInfo.FamilyName));
                break;
        }
    }
}