5

The two seem the same to me:

  • a convenience is a helper
  • a helper is a convenience

When is it good to use the one or the other term?

13

Functions have two main purposes: aiding code reusability and breaking down a task into smaller logical units. Functions that do not aid code reusability are helper functions; their sole purpose is to "help" a single function by cleaning the code and making the logic clearer. The opposite of a helper function is a library function, which focuses on code reuse.

Source: Cunningham & Cunningham

whereas:

A convenience function is a non-essential subroutine in a programming library or framework which is intended to ease commonly performed tasks.

Source: Wikipedia

So:

  • A helper function which is not a convenience function would be any function which breaks down a task into smaller logical units without aiding code reusability and without having a goal of easing a commonly performed task.

    For example, a specific task T may require to perform action A and action B. Refactoring may lead to three functions: T(), A() and B(), given that it may not make too much sense to call A() without calling B() just after that.

  • A convenience function which is not a helper function would ease commonly performed tasks without breaking down a task into smaller logical units.

    For instance, if many persons are calling the task T₁ immediately followed by the task T₂, a convenience function would be T₃ which will simply call T₁, then T₂. Here, T₃() doesn't "help" a single function, and doesn't break down any task into smaller logical units: it simply regroups two currently used tasks.

4

They're just terms. In most scenarios, they are interchangeable.

In general though, a convinience function is one that just helps you do something you can already do, albeit with less typing. An overload that forwards with the usual defaults for example.

A helper often is some new functionality that doesn't really belong somewhere so it's off on its own, but still helps out.

0

At least as I'd use them, the two are actually more or less opposites.

A convenience function is (at least close to) a pure interface function--something that only provides a nicer, more convenient interface for client code to use. A classic example would be a front-end for a recursive function such as a Quicksort:

Quicksort(array a) { 
    QuicksortImplementation(a, 0, a.size());
}

A helper function, by contrast, normally isn't visible at all. In object oriented programming, for example, it'll typically be implemented as a private method/member function.

class X { 
    public void foo() { 

        // beginning of `foo`

        if (some_condition)
            helper();

        // remainder of foo
    }

    private void helper() { 
        // do some relatively minor service for foo()
    }
};

In both cases, however, there is usually the basic idea that the helper/convenience function provides only a small part of the overall functionality. There is also at least the connotation that the helper/convenience function is really only intended to be used in conjunction with one other primary function. It's usually not particularly general or reusable, just a part of its primary function that needs to be (or is more convenient when) written as a separate function for one reason or another.

  • The difference is that the "helper" doesn't do something that would be considered "useful" by code outside, because it is so specialized. While the convenience function is very useful to outside code; given the choice between calling QuickSort and QuickSortImplementation I would be much more like to call the convenience method because it is so convenient. I'd never call the helper even if it was public because it isn't useful except as a building block. – gnasher729 Jan 31 '17 at 0:23

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.