Sign in to follow this  
LiziPizi

For what functions we use the word "On"?

Recommended Posts

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? 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this