2

While exploring the dank catacombs and dusty dungeons of our legacy code, I came across this:

FormatString formatString = new FormatString();
if (formatString.containsAlpha(UPCE) != -1) 
{
    UPCLen = 11;
}

Am I missing something, or is my reaction to this, namely: How can formatString contain anything? Nothing has been assigned to it...It will always be -1, assuming that indicates 'not found'" correct?

UPDATE

In answer to the general confusion evident in the comments, I thought FormatString was some haywire stone ages .NET thing (this project uses .NET 1.1), but you're right - this is a homegrown class. Here is the constructor:

public FormatString()
{
}

...and the containsAlpha() method:

public int containsAlpha(string strToCheck)
{
    const string ALPHA_CHARS = "abcdefghijklmnopqrstuvwxyz";
    try
    {
        char[] tmpCharArry = ALPHA_CHARS.ToCharArray();
        return strToCheck.ToLower().IndexOfAny(tmpCharArry);
    }
    catch(Exception ex)
    {
        Duckbill.ExceptionHandler(ex, "FormatString.containsAlpha");
        return 0; // not -1?
    }
}

Now I ask you: Is "FormatString" a wrong-headed name for this class, or what? I found it very misleading (obviously).

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    What _is_ `FormatString` at all? – Tim Schmelter Sep 13 '13 at 23:20
  • What's a `FormatString`? The constructor could be doing anything for all we know. – Mike Zboray Sep 13 '13 at 23:20
  • 1
    Tim's right, `FormatString` is not part of the CLR. Perhaps it is has some nasty critters living inside it. – Cᴏʀʏ Sep 13 '13 at 23:21
  • 1
    Try right-clicking on `FormatString` and select `Go To Definition`. See where it goes. – LarsTech Sep 13 '13 at 23:26
  • FormatString may have a value on instantiation, you will have to look at the source code, check out the new constructor to see what it does. Looks like you got a company internal business object, FormatString is not part of the .NET Framework – Brian Ogden Sep 13 '13 at 23:26
  • I'm assuming it's a helper class that checks whether its passed argument contains Alphas. It's odd that it's not static, but, ... – dcaswell Sep 13 '13 at 23:26
  • 1
    This is what you get when you have savvy junior consultants offhandedly telling stone-age autodidacts to organize their string formatting functions in a separate class, while insisting that it should not be static because that's how they learned it in school, completely ignoring the part where the project doesn't use any kind of dependency injection. – Jan Van Herck Sep 13 '13 at 23:45
  • 1
    I'm not sure who is meant to be who here, but the "stone-age autodidact" does kind of fit me. – B. Clay Shannon-B. Crow Raven Sep 13 '13 at 23:48

4 Answers4

3

For all we know, it could be something like this:

public class FormatString
{
    public int containsAlpha(object foo)
    {
        return 0;
    }
}

That would satisfy the example you showed. It doesn't even need a constructor definition.

You should right-click and "go to definition" to find out what it really does.

UPDATE

Based on your updated details, If you could use a newer .NET Framework, I'd say you could replace the entire function with this:

using System.Linq;

public static bool ContainsAlpha(string s)
{
    return s != null && s.Any(Char.IsLetter);
}

But since you are stuck, this should work just as well:

public static bool ContainsAlpha(string s)
{
    if (s == null) return false;

    for (int i = 0; i < s.Length; i++)
        if (Char.IsLetter(s, i))
            return true;

    return false;
}

Or alternatively:

public static bool ContainsAlpha(string s)
{
    if (s == null) return false;

    for (int i = 0; i < s.Length; i++)
        if (Char.IsLetter(s[i]))
            return true;

    return false;
}
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
2

Is "FormatString" a wrong-headed name for this class, or what?

In my opinion, yes.
It should be named like StringUtils or StringHelper or something like that.
And also it should be static.

Update
What's more, I'd prefer it to be an extension method. Then it would be like:

string UPCE = // whatever
if(UPCE.ContainsAlpha())
{
 // ...
}
alex.b
  • 4,547
  • 1
  • 31
  • 52
  • @MirroredFate, not sure regarding `validator`, to be honest. Because usually [validator does Validation](http://farm5.staticflickr.com/4119/4885545985_c55093102d_z.jpg), I mean it tells whether things are Ok or not, which is not quite the case here. – alex.b Sep 14 '13 at 00:22
  • I agree that it's not perfect, but as a container of static methods it **would** be validating *something*, even if that was just that the passed-in string has alphabetic characters. `StringUtils` is just so broad and `StringHelper` has the same problem, in addition to sounding childish (not that sounding childish should be immediate disqualification). – MirroredFate Sep 14 '13 at 00:30
  • I agree that `*Utils` and `*Helper` is broad, I don't like names like this too. – alex.b Sep 14 '13 at 00:33
  • Though I can't agree that `to validate` is better term here. I'd say the `StringChecker` could be used, but ... Let's say, it heavily depends on the context, without it any name is more or less the same (lu|su)cky. And we don't have the context here. – alex.b Sep 14 '13 at 00:36
1

It is possible that FormatString is initialized from some form of configuration. I would check the constructor. That is the only way this makes sense (to me).

Ziffusion
  • 8,779
  • 4
  • 29
  • 57
1

Even though it's clearly weird (shouldn't "contains..." return a boolean?) and counter-intuitive (I have created an "empty" FormatString, i.e. nothing passed through its constructor, why should it do anything?), it doesn't strike me as entirely nonsensical.

"containsAlpha" might as well mean: Check if parameter 1 (UPCE in this case) contains alphabetic (probably alphanumeric) characters. So FormatString would be some generic class that checks for basic characteristics of how strings are formatted, i.e. do they contain numbers, letters, special chars etc.

It's also possible that FormatString is some specialized class (check the namespace?) that checks for the format of, e.g. UPCs, and would probably better be called UPCFormat, UPCFormatVerifier etc.

If I'm not completely mistaken, I don't recognize this as a .NET framework function. Which target framework version is this? Do you have the source of FormatString? What does it say?

Anyway, a beautiful piece of strange code. ;-)

Ziffusion's answer also makes sense, look at the construcor code. Perhaps it does more than it should, some initialization either in regard to some default state, or, even worse, something involving static global state stuff, which you never find out unless you run the whole system.

Edit: Now read your edit - as it turns out, I was right that FormatString does "generic string format checking." Probably StringFormat would be a slightly better name, but all in all I'd say: If you want to check the format of UPCs, have either an UPC class with a static factory/builder method which builds an UPC object from a string, including format checks, or create a UPCFormatCheck class which solely does the job of checking specific formats. This way, you avoid all too generic names that could mean anything. (In that class you can obviously use some cool LINQ-oneliner to do the actual check, as suggested.)

tmh
  • 1,385
  • 2
  • 12
  • 18
  • .NET 1.1 - that's what I thought it was just some obsolete/deprecated thing. I should have known - the naming conventions used by the previous programmers make Dr. Seuss seem staid and conventional by comparison. – B. Clay Shannon-B. Crow Raven Sep 13 '13 at 23:38
  • Oh. Then you're really stuck with introducting `StringUtils` or something with a static helper method. – tmh Sep 13 '13 at 23:47