Article 6JXYR CodeSOD: A Split Purpose

CodeSOD: A Split Purpose

by
Remy Porter
from The Daily WTF on (#6JXYR)

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.

proget-icon.png [Advertisement] Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments