Jump to content
  • Advertisement
Sign in to follow this  
LiziPizi

For what functions we use the word "On"?

This topic is 628 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!