Advertisement

For what functions we use the word "On"?

Started by March 22, 2017 04:25 PM
1 comment, last by frob 7 years, 8 months ago
Unity uses the word "On" a lot in its function like: OnTriggerEnter, OnCollisionEnter, OnDrawGUI, OnDrawScene, OnDrawInspector, and more.
Its sounds good to add the word "On" in my own functions like: OnEndLevel, OnStartLevel, OnArrowFire, OnJump.
It is a good habit or bad habit to use it like that?

Those On* functions are event handlers that are called by the Unity engine. An event handler is just a specific type of 'callback function'. So, are your functions also event handlers? If not, I would suggest some other naming convention. If you're unsure, please explain how you plan on using those functions.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Advertisement

The OnEvent naming convention is quite old, I remember seeing it in the 1980s for exactly the same thing and it likely goes back longer than that. It is a function that is called whenever event happens. There are other options, like adding PreEvent and PostEvent, or multiple points with Event (before event processing) Eventing(called during event processing), and Evented (called after completion), for example Click(), Clicking(), and Clicked().

There are many common naming conventions: functions names are verbs if they do something, nouns if they are accessors/mutators; objects are nouns; outside of objects use a NounVerb or NounVerbNoun pattern; and so on.

The best thing about standards and conventions are that there are so many to choose from. Pick one and stick with it.

This topic is closed to new replies.

Advertisement