5
\$\begingroup\$

The entire flow depends on parameters. It's all in the same method with an if/else, but I want to make the code a bit more clear. How can I functionally decompose this method into many?

private void BindTreeView(IEnumerable<Opcion> OptionList, TreeNode parentNode)
{
    var nodes = OptionList.Where(x => parentNode == null ? x.ParentID == 0 : x.ParentID == int.Parse(parentNode.Value));

    if (_pagemode != BoPage.PageMode.View)
    {
        foreach (Opcion node in nodes)
        {
            TreeNode newNode = new TreeNode();
            newNode.Text = (node.ParentID == 0) ? string.Format(@"<span class=""Tema"" id=""Tema{0}"">{1}</span>", node.OpcionID.ToString(), node.Descripcion.ToString()) : string.Format(@"<span class=""SubTema"">{1}</span>", node.ParentID.ToString(), node.Descripcion.ToString());                      
            newNode.Value = node.OpcionID.ToString();
            newNode.ToolTip = node.Descripcion.ToString();

            if (parentNode == null)
            {
                TreeViewPalabrasClaveOpciones.Nodes.Add(newNode);
            }
            else
            {
                if (node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0)
                {
                    newNode.Checked = true;
                    parentNode.Expand();
                }
                parentNode.ChildNodes.Add(newNode);
            }
            BindTreeView(OptionList, newNode);               
        }

    }
    else
    {
        foreach (Opcion node in nodes)
        {
            if (ExistKeyWord(node))
            {                
                TreeNode newNode = new TreeNode();
                newNode.Text = (node.ParentID == 0) ? string.Format(@"<span class=""Tema"" id=""Tema{0}"">{1}</span>", node.OpcionID.ToString(), node.Descripcion.ToString()) : string.Format(@"<span class=""SubTema"">{1}</span>", node.ParentID.ToString(), node.Descripcion.ToString());                      
                newNode.Value = node.OpcionID.ToString();
                newNode.ToolTip = node.Descripcion.ToString();

                if (parentNode == null )
                {
                    TreeViewPalabrasClaveOpciones.Nodes.Add(newNode);
                }
                else
                {
                    if (node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0)
                    {
                        newNode.Checked = true;
                        parentNode.Expand();
                    }

                    if (node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0)
                    { 
                        parentNode.ChildNodes.Add(newNode);
                    }

                }
                BindTreeView(OptionList, newNode);
            }
        }

    }


}
\$\endgroup\$
3
\$\begingroup\$

I want to make the code a bit more clear

I would start by removing one of the if branches and customize the loop to do the job according to the parameters because both loop are nearly identical.

You can create two helper variables for the conditions and act appropriately:

var canAddTreeNote = _pagemode != BoPage.PageMode.View || ExistKeyWord(node);

if (!canAddTreeNote)
{
    continue;
}

and for adding child nodes something like this:

var canAddChildNote = 
    _pagemode == BoPage.PageMode.View 
    && ExistKeyWord(node)
    && node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0;

if (canAddChildNote) 
{
    parentNode.ChildNodes.Add(newNode);
}

by their powers combined you'll get:

private void BindTreeView(IEnumerable<Opcion> OptionList, TreeNode parentNode)
{
    var nodes = OptionList.Where(x => parentNode == null ? x.ParentID == 0 : x.ParentID == int.Parse(parentNode.Value));


    foreach (Opcion node in nodes)
    {

        var canAddTreeNote = _pagemode != BoPage.PageMode.View || ExistKeyWord(node);

        if (!canAddTreeNote)
        {
            continue;
        }

        TreeNode newNode = new TreeNode();
        newNode.Text = (node.ParentID == 0) ? string.Format(@"<span class=""Tema"" id=""Tema{0}"">{1}</span>", node.OpcionID.ToString(), node.Descripcion.ToString()) : string.Format(@"<span class=""SubTema"">{1}</span>", node.ParentID.ToString(), node.Descripcion.ToString());                      
        newNode.Value = node.OpcionID.ToString();
        newNode.ToolTip = node.Descripcion.ToString();

        if (parentNode == null)
        {
            TreeViewPalabrasClaveOpciones.Nodes.Add(newNode);
        }
        else
        {
            if (node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0)
            {
                newNode.Checked = true;
                parentNode.Expand();
            }

            var canAddChildNote = 
                _pagemode == BoPage.PageMode.View 
                && ExistKeyWord(node)
                && node.PalabrasClaveOpciones.Where(c => c.KeywordID == _idKeyWord).Count() > 0;

            if (canAddChildNote) 
            {
                parentNode.ChildNodes.Add(newNode);
            }
        }
        BindTreeView(OptionList, newNode);               
    }
}

How can I functionally decompose this method into many?

When you apply the above refactoring I think it's no longer necessary to create more methods.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.