Let’s say you had input in the form of field=value, and you wanted to pick that “value” part off. In C#, you’d likely just use String.Split and call it a day. But you’re not RK‘s co-worker.

public string FilterArg(string value)
{
    bool blAction;
    if (value.Contains('='))
        blAction = false;
    else
        blAction = true;

    string tmpValue = string.Empty;

    foreach (char t in value)
    {
        if (t == '=')
        {
            blAction = true;
        }
        else if (t != ' ' && blAction == true)
        {
            tmpValue += t;
        }
    }
    return tmpValue;
}

If the input contains an =, we set blAction to false. Then we iterate across our input, one character at a time. If the character we’re on is an =, we set blAction to true. Otherwise, if the character we’re on is not a space, and blAction is true, we append the current character to our output.

I opened by suggesting we were going to look at a home-grown split function, because at first glance, that’s what this code looks like.

It does the job, but the mix of flags and loops and my inability to read sometimes makes it extra confusing to follow.

[Advertisement] Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.

Remy Porter

Source link

You May Also Like

Ripping the Headlines Today – Paul Lander, Humor Times

Making fun of the headlines today, so you don’t have to The…

You’ll Hate Me For These Terrible Jokes (25 Photos)

Laura Lee Source link

The strangest things women have said to men after a night in bed (18 GIFs)

Jacob Source link

Tattoo fails that remind you to think before you ink (30 Photos)

Adam Source link