1

How would one name a function type called by a certain event? Instances of this function type would get called when a certain key is pressed. It isn't strictly an event handler type, because it doesn't have to handle the event. It might simply just want to be aware of the event. There can be multiple functions with this type listening for the same keystroke event.

I currently am using SpecificKeystrokeListener although that's not really correct as it doesn't listen, it is merely being notified.

For what it's worth, I am using Rust - though this applies to other languages as well. As an example:

type ResetFn = fn (this: *const device) -> ReturnStatus;

  • 1
    I'm guessing it does more than just get notified about an event... what does it actually do? If SpecificKeystrokeListener actually writes the keystrokes to a buffer or something, maybe name it WriteKeystrokeToBuffer. I'm not sure why the relation to the event needs to be in the name. Maybe you can give more detail that would explain this? – FrustratedWithFormsDesigner Oct 8 '14 at 19:27
  • @FrustratedWithFormsDesigner This is just the type name, not the name of the actual function. It might push it to a buffer, or it might just count how many keys were pressed, or do a whole lot of other things. – Jeroen Oct 8 '14 at 19:30
  • 2
    So you just want to know what such a function is called? It has various names: delegate, event handler, first-class function. The software pattern is called "observer." – Robert Harvey Oct 8 '14 at 19:34
  • @RobertHarvey I think observer is indeed what I am looking for. You can add it as an answer if you wish. – Jeroen Oct 8 '14 at 19:36
  • 1
    @JeroenBollen: Yes, it sounds like "Observer" fits what you're describing. – FrustratedWithFormsDesigner Oct 8 '14 at 19:43
3

The convention in Windows is

ObjectName_EventName

Example:

ProductSelector_Click()

It is done this way in Windows (.NET, specifically), because it follows a generic signature that allows you to do message passing:

public void ProductSelector_Click(object sender, EventArgs e)

That said, if your method is merely a delegate or first-class function (and you don't need the sort of "patching" capability provided here), you can pretty much name it whatever you want.


More generally, this software pattern is called "Observer."

0

Given the way OP is phrased I think he's looking for "Subscriber", although that moniker is more often used in Publish–subscribe scenarios. Traditionally in event handling libraries they're called "Listeners" (as the OP code snippet demonstrates), but descriptions of the Observer pattern use "Subscriber", for example, here: http://www.oodesign.com/observer-pattern.html

See https://stackoverflow.com/questions/15594905/difference-between-observer-pub-sub-and-data-binding for a discussion of the differences between these patterns.

0

If the function listens for an event and then calls another function to handle it, I would call that function [Event]Dispatcher (replace [Event] with the name of the event. Think of the old time telephone dispatchers that you call into and ask to be connected to so and so. It seems like the perfect fit for what you're doing.

  • It doesn't handle the event, or call any function handing the event. – Jeroen Oct 9 '14 at 14:42
  • Oh well I'm confused...what precisely does it do? Why is it subscribing to the event if it doesn't do anything? – Michael Brown Oct 9 '14 at 15:01
  • It does do something, it just doesn't handle the event. It might simply take note of the event. – Jeroen Oct 9 '14 at 15:32
  • Ah gotcha...well I'd go with listener as well. – Michael Brown Oct 9 '14 at 15:50
  • It isn't really listening though, as it isn't actively checking for an update, but rather is being notified. – Jeroen Oct 9 '14 at 15:53

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