Dim
Dim

Reputation: 4807

Simplify bool function

I wrote this function that returns true/false. My head is stuck on this return algorithm and I can not manage to simplify it. Can you please help me? I know It can be simpler regarding return values and not have to have so much return values, please help me simplify it.

private bool CheckSourseLang(string flnfo)
    {
        if (CUR_DOWNLOAD.Equals("ST"))
        {

            if (flnfo.eqauls("English"))
            {
                for (int i = 0; i < WW_Langs.Count; i++)
                {
                    if (p.Contains(WW_Langs[i]))
                    {
                        return true;
                    }
                }
            }

            else
            {
                return true;
            }

        }

        else
        {
            return true;
        }

        return false;
    }

Upvotes: 0

Views: 218

Answers (4)

user1196549
user1196549

Reputation:

The only case that returns false is when the search in the array WW_Langs is attempted and fails. Let us make this explicit:

private bool CheckSourceLang(string flnfo)
{
    if (CUR_DOWNLOAD.Equals("ST") && flnfo.Equals("English"))
    {
        // Lookup the language string
        int i;
        for (i = 0; i < WW_Langs.Count && !p.Contains(WW_Langs[i]); i++)
        {
        }
        if (i == WW_Langs.Count)
        {
           // Array exhausted, the language string was not found !
           return false;
        }
    }

    return true;
}

Or, using more modern constructs:

private bool CheckSourceLang(string flnfo)
{
    if (CUR_DOWNLOAD.Equals("ST") && flnfo.Equals("English") &&
       !WW_Langs.Any(Lang => p.Contains(Lang)))
    {
        // Lookup of the language string failed !
        return false;
    }

    return true;
}

IMO, this is more readable than return (complex Boolean expression); as it clearly shows what the false case is. Would be even better if there was a None predicate implementing !Any, to avoid a negation.

Upvotes: 2

Ghasem
Ghasem

Reputation: 15573

    private bool CheckSourseLang(string flnfo)
    {
        if (CUR_DOWNLOAD.Equals("ST")) return true;
        return !flnfo.Equals("English") || WW_Langs.Any(t => p.Contains(t));
    }

Upvotes: 3

WAQ
WAQ

Reputation: 2626

 private bool CheckSourseLang(string flnfo)
        {
            if (!CUR_DOWNLOAD.Equals("ST"))
                return true;
            if (!flnfo.Equals("English"))
                return true;
            for (int i = 0; i < WW_Langs.Count; i++)
            {
                if (p.Contains(WW_Langs[i]))
                {
                    return true;
                }
            }
            return false;
        }

Upvotes: -1

ps2goat
ps2goat

Reputation: 8475

Here's a dotnetfiddle for testing. https://dotnetfiddle.net/kCDIUy

private bool CheckSourseLang(string flnfo)
{
    return  !CUR_DOWNLOAD.Equals("ST") || 
             (!flnfo.Equals("English") || 
             WW_Langs.Any(w => p.Contains(w) ) );
}

Upvotes: 3

Related Questions