Home > Mobile >  How to optimize method with nested if condition
How to optimize method with nested if condition

Time:10-26

Hello I want to optimize this method with many nested if else with performance side or structure side also.

Here I have many condition for every case I did a query to my datasource this is the principle.

The above method works, but I would like to know if there is any way to make it more readable and less of a performance cost?

Here is my code :

if ((inCheckbox != null) && (inCheckbox == "true"))
{
    if ((inCheckboxQu != null) && (inCheckboxQu == "true"))
    {
        if (wordArraySplit.Length > 1)
        {
            var request = from qu in dataResultInitial
                        from p in q.Pannea
                        where wordArraySplit.All(word => p.Title.Contains(word))
                        select q;

            dataResult = dataResult.Union(request);
        }
        else
        {
            var valueArray = wordArraySplit[0];
            var query = dataResultIni.Where(qu=> qu.Pannea.Any(p => p.Title.Contains(valueArray)));
            dataResult = dataResult.Union(query);
        }
    }
    else
    {
        if (wordArraySplit.Length > 1)
        {
            dataResult = from qu in dataResultInitial
                         from p in qu.Pannea
                         where wordArraySplit.All(word => p.Title.Contains(word))
                         select q;
        }
        else
        {
            var valueArray = wordArraySplit[0];
            dataResult = dataResultInitial.Where(qu=> qu.Pannea.Any(p => p.Title.Contains(valueArray)));
        }
    }
}

CodePudding user response:

To improve performance with this type of checks what you usually do is use the checks that fail most often first for example

if test1 and test2: ...

then test1 should be the one most often failing so you dont have to evaluate test2

To make it more redable I usually compine the tests in a different function and return true or false and keep the logic in the main function like this :

if functiontest():
  #do_something

def functiontest():
  if test1 and test2 and test3 and test3:
    return True
  else:
    return False

In your case you need two of this function since you basically have 4 final stages.

dummy code is in python style

Also look at this for another idea they suggest making a dictionary with possible combinations: readable nested if statements

CodePudding user response:

This should work for you:

if ((inCheckbox == null) || (inCheckbox != "true"))
    return;

if (wordArraySplit.Length > 1)
{
    dataResult = from qu in dataResultInitial
    from p in qu.Pannea
    where wordArraySplit.All(word => p.Title.Contains(word))
    select q;
    
    if ((inCheckboxQu != null) && (inCheckboxQu == "true"))
        dataResult = dataResult.Union(request);

}
else
{
    var valueArray = wordArraySplit[0];
    var query = dataResultIni.Where(qu=> qu.Pannea.Any(p => p.Title.Contains(valueArray)));
    if ((inCheckboxQu != null) && (inCheckboxQu == "true"))
        dataResult = dataResult.Union(query);
}

i don't know if the return fits the rest of your function.

  • Related