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

Grand Jury Testimony: Mike Pence – Marilyn Sands, Humor Times

Was Star Witness Mike Pence’ Grand Jury testimony an Interrogation or a…

And now, for something completely different: Dinosaur Memes (27 Photos)

And now, for something completely different: Dinosaur Memes (27 Photos) Jacob Source…

People reveal the greatest Halloween costume they ever pulled off (28 Photos)

I asked you folks over on theCHIVE’s IG page what was the…

John Deering for Jan 07, 2024 – John Deering, Humor Times

John Deering is chief editorial cartoonist for the Arkansas Democrat-Gazette, the state’s largest…