39

Maybe a little tongue-in-cheek, but as I can't find this answer anywhere through Google, so to ensure Software Engineering has the answer:

What is a helper?

I have seen the name being used everywhere (module names, class names, method names), as if the semantics were deep and meaningful, but in the context of Computer Science (although I don't have a degree in it), I've never seen a description or definition anywhere!

Is it a design pattern? Is it an algorithm? I once worked on a program in which the module and class were both called somethingsomethinghelper (where somethingsomething was fairly generic too) and I promptly renamed it to something that made sense to me, but I feel like I'm missing something here!

  • 7
    Helper is what you call something when you don't know what to call it but you know one of its friends. Kind of like calling you 'friend of Zack' instead of Aaron. Double plus ungood. – david.pfx Jul 9 '14 at 11:29
  • A helper is any private member, up to isomorphism. – Thomas Eding Jul 10 '14 at 4:31
  • @ThomasEding sorry to come back 3 years later - but I've seen lots of "public" members called "helper", including interfaces. I'd like a source on your definition (the higher quality the better) because that would definitely give me a sense for more code smells. – Aaron Hall Sep 8 '17 at 15:13
59

A Helper class is a lesser known code smell where a coder has identified some miscellaneous, commonly used operations and attempted to make them reusable by lumping them together in an unnatural grouping. Successive developers have then come onto the project and not realised that the helper class exists, and have consequently rewritten the same common operations, or even created more Helper classes.

But seriously, the main problem with Helper classes is that they are usually operations that act on a specific class, which obviously means in OO terms that they are suffering from an acute case of Feature Envy. This failure to package the behaviour with the data it acts on is why developers so often (in my experience) fail to find it.

In addition to this, as you have already identified SomethingSomethingHelper is actually a terrible name. It is undescriptive, and gives you no real inkling of what sort of operations the class does (it helps?), which also means that it's not obvious when adding new behaviours whether they belong in the Helper class or not. I would break up such classes along the lines of related behaviour that logically group together, and then rename the new classes to reflect what it does.

  • 9
    Pretty sure that SomethingSomethingHelper is not the actual name of the class. Whether it's a code smell or not would depend on how specific the helper class is, as helper classes like Math are not a code smell at all. – Robert Harvey Jul 8 '14 at 16:58
  • 9
    @RobertHarvey - Enh, most of the ones I've seen have been named SomethingSomethingHelper. Hell, I'm looking at a class right now named HelperMethods in the <company>.Helpers namespace. To me, a Helper class is in the same bucket as *Manager. – Telastyn Jul 8 '14 at 18:05
  • 8
    @Telastyn: Perhaps it's an experience thing, then. I've looked through the helper classes in my current project (there are several), and they all have meaningful names. There's only one that's suffixed with Helper, and I think that's to disambiguate it from a class in the .NET Framework having the same name. I'd consider *Helper acceptable if * was something meaningful. HelperMethods is just a failure of imagination; there ought to at least be specific conceptual buckets. – Robert Harvey Jul 8 '14 at 18:55
  • 3
    @RobertHarvey - You're probably right. All of my career using modern languages has been cleaning up train wrecks. – Telastyn Jul 8 '14 at 19:02
  • 1
    There is a subset of these, called "helper functions", that are actually useful and a good pattern. For example, a common set of lines that would otherwise need to be repeated within a function - one example of where they show up is when you need a do..while loop in Python, which the language doesn't support (see the second example here). Another would be an if/elif/.../else structure, but need to repeat a case at the top and bottom. If possible they should be made local to that function, though, and generally not actually called "helper". – Izkata Jul 8 '14 at 21:22
5

A helper is a harmless additional class or method, as long as it complements an external component. When it does the contrary, then it indicates bad design because the code has been excluded from its authority, if there is any authority at all.

Here is an example of a harmless helper, I use a method called FindRep that counts the number of leading zeros.

digits = digits.Remove(0, TextHelper.FindRep('0', digits, 0, digits.Length - 2));

The helper method is very simple, but very inconvenient to copy-paste around and the framework does not provide any solution.

public static int FindRep(char chr, string str, int beginPos, int endPos)
{
    int pos;

    for (pos = beginPos; pos <= endPos; pos++)
    {
        if (str[pos] != chr)
        {
            break;
        }
    }

    return pos - beginPos;
}

And here is an example of a bad helper:

public static class DutchZipcodeHelper
{
    public static bool Validate(string s)
    {
        return Regex.IsMatch(s, @"^[1-9][0-9]{3}[A-Z]{2}$", RegexOptions.IgnoreCase);
    }
}

public class DutchZipcode
{
    private string value;

    public DutchZipcode(string value)
    {
        if (!DutchZipcodeHelper.Validate(value))
        {
            throw new ArgumentException();
        }

        this.value = value;
    }

    public string Value
    {
        get { return value; }
    }
}
-1

My company used to use the Base class / Helper class methodology where each object would have two classes. You would have a Person class that contained all of the class properties and definitions and a PersonHelper class that contained all of the methods, SQL statements and Logic that manipulated the Person class. This worked well for us because all of our applications use SQL statements to manipulate data and it was very easy for us to find and modify the SQL statements as needed.

We have since moved on and now put everything in the Person / Base class. We quit using the Helper naming convention because we wanted to have less files in our projects. Also, the length of some of the class names were getting out of control. lol.

Not a great example but you get the idea.

s = CompanyName.PersonHelper.GetPerson()
s = CompanyName.Person.GetPerson()

I'm not saying that using the helper naming convention is the perfect solution but it did work for us for a few years.

  • 2
    You didn't explain why. – Robert Harvey Jul 9 '14 at 18:00
  • 2
    Yes, I'd like that explanation too. – Aaron Hall Jul 9 '14 at 18:11
  • @AaronHall Consider it a good thing that you do not understand their choice. – Leopold Asperger Jul 10 '14 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.