2

Sometimes I'll see some functions use "on" as a prefix, for example, on a button listener:

onButtonPressed();

and there are other examples:

onStateChanged();
onNotified();
onResume();

Does the prefix "on" have an implied semantic meaning behind it?

  • 2
    In this context, on <something> means when <something>. Please note that asking "Why" is on used in this way?" will probably be considered off-topic here. – Kilian Foth Oct 6 '15 at 7:43
  • 4
    Preposition of time like in 'on Sunday' or 'on Christmas'. Mostly 'on' is used only for specific dates but sometimes can be used together with more general events like 'on arrival'. So maybe slightly questionable grammar use here, but we have this often when naming things and try to keep it short. – thorsten müller Oct 6 '15 at 7:54
  • 2
    its a prefix used to denote an event handler. On<Event Name> is an easily understood and grokkable terminology. IIRC it came to prominence with MFC and has stuck. – gbjbaanb Oct 6 '15 at 7:56
9

Prefix "on" is most often used to indicate that method is intended to be used as a callback, i.e. not called directly, but set as a handler for some event.

For example, when you write method named onClick, you probably won't call it directly, but rather expect that GUI toolkit will call it once user clicks a button.

  • I wonder why the word "when" wasn't chosen instead. Apart from being used to the "on" idiom, "when" seems a little more obvious and clear - whenStateChanged(), whenNotified(), etc. – jbyrd Mar 7 '18 at 16:24
  • 1
    @jbyrd I think if you look at the 'tense' of the words it changes its use context for example; whenStateChanged() would be onStateChange and whenNotified becomes onNotice. It changes the context of the word and better describes the situation that will happen , not that it has happened. Hope that helps. – Jessycormier Sep 7 '18 at 13:20
  • @Jessycormier - ah, that totally makes sense; thanks for the input! Also "on" happens to be half the letters as "when", so that's nice. – jbyrd Sep 7 '18 at 15:36
6

Edited to qualify and expand my answer after the conversation in the comments with @MetaFight


In just about every language I've seen with events (JavaScript, ActionScript2/3) the on prefix is used to signify an event handler procedure and an emit, fire, or raise prefix is used on a method which elicits an event. (On might also be used in a property name to signify the attachment of a callback, such as in the onclick HTML attribute.)

C#, on the other hand, appears to break this this convention somewhat.

When the On prefix is used on a method in a base class, it will actually fire an event:

public class Base
{
    public delegate void OpeningEventHandler(Object sender, EventArgs e);
    public event OpeningEventHandler Opening = delegate {};

    protected virtual void OnOpening(EventArgs e)
    {
        Opening(this, e);
    }
}

When this method is overridden in a derived class, the override is supposed to handle the event, also calling the base implementation:

public class Derived : Base
{
    protected override void OnOpening(EventArgs e)
    {
        // handle event here
        base.OnOpening(e);
    }
}

This is presumably used to enable a deriving class to cancel propagation of the event (by not calling the base method).

From the MSDN documentation for the Form.Onload method:

Remarks

...

The OnLoad method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors:

When overriding OnLoad in a derived class, be sure to call the base class's OnLoad method so that registered delegates receive the event.

There's a related question on SO.

  • Methods with the On prefix are typically event handlers. On the other hand methods with the Fire or Emit prefixes are typically responsible for triggering an event not handling them. – MetaFight Oct 6 '15 at 16:15
  • 1
    In non-C# languages I'm sure that's true, but C# uses the convention I've outlined where the event-firing function begins with On. I agree it seems backwards, but that's the convention. My comment (and sorry if it wasn't clear) was that the firing function would be better prefixed with Emit or Fire (so I agree with you on that). – paul Oct 6 '15 at 21:01
  • I just looked at the MSDN documentation for the WinForms Form.OnLoad(...) method. To my surprise it is the responsibility of the base class implementation of this method to fire the related event. Derived implementations are merely event handlers. If you ask me that's a pretty inconsistent pattern. And it explains the awkward naming. – MetaFight Oct 7 '15 at 14:23
  • @MetaFight Yeah, when I initially encountered the base-class convention it really threw me for a loop, having been used to On prefixing event handlers. I hadn't known the convention was to treat it as a handler in derived classes (having mainly done base-class implementations in my C# work and only seeing examples of such). That is quite awkward. – paul Oct 7 '15 at 15:43
  • 1
    A bit nit-picky, but the convention belongs to .NET, not C#. Those methods are available to other .NET languages. – rory.ap Jul 19 at 14:18
3

It's just one of the many meanings of the English word word "on", used as a preposition. It indicates when the action occurs. So onButtonPressed() is called when the button is pressed.

The function could be re-named whenButtonPressed(), but that's longer and not idiomatic

2

"OnXXX" is typically something with events, but different platforms have different naming conventions, so the exact role depends on the language, which can get confusing.

In .net UI classes, a method name with the prefix "On" is a method which causes an event to be fired. For example, Button.OnClick() is the method which fires the Click-event. The OnXXX methods are protected, so it is only the object itself which can fire the events. This pattern holds for both web forms, windows forms and WPF.

But in asp.net web control markup, the attributes named OnXXX has a different meaning since they registers an event handler for the event. Eg. OnClick="Btn1_Click" registers the method Btn1_Click as a handler for the Click event. In other words the OnClick attribute on a web control does not correspond directly to the OnClick method. Rather it corresponds to the code Click += Btn1_Click. Honestly, this is somewhat confusing, and in XAML-markup the convention is changed, so the name of the attributes which attach events does not have the "On"-prefix but match the name of the event directly, eg.: Click="Btn1_Click".

In JavaScript, the conventions are different. Here onXXX is the name of an event handler. Eg. button.onclick is a method which is called when the click event is fired (rather then a method which fires the event as in .net). The eventhandler atttributes in html are also named onXXX and attaches an eventhandler, eg. onclick="handleClick()" registers a handler for the click event (as in Asp.net).

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.