Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Naming Convention case

Discussion in 'General Discussion' started by mahdiii, Dec 26, 2018.

  1. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    Hi. When I want to shoot or use an item, I check some conditions and then soot.
    Which one is better and more suitable to name the functions?

    Code (CSharp):
    1. TryShooting
    2. TryShoot
    3. TryToShoot
    4. ShootIfCan
    5. TryUseItem
    6. TryToUseItem

    Code (CSharp):
    1. public class Shooter{
    2.    public void TryShooting(){
    3.     // if some conditions are satisfied then call it
    4.            Shoot();
    5.    }
    6.    private void Shoot(){}  // It is private
    7. }
    8.  
    Also, when a player wins or loses, I want to call a function that handle many things like playing animation, play sound, compute and show score, ... . Which name is suitable?
     
  2. Deleted User

    Deleted User

    Guest

    The one you find suitable.
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    Make sure, your function name describes what it does.
    If you have multiple functions inside one function, then again, think what it does, and how collectively you can name Tha function. Use any name, as long makes sense. Plus, if is not descriptive enough, add 3x slashes /// above defined function. This will automatically add info to your function. Put more detailed description there.
     
  4. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    Thanks but what do you name it in your games? LOL
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    I name as it fits.
    There is no universal naming convention.
    Must be meaningful, that for sure.

    Just use what you think is right now.
    You can later refactor (change names) all at once, for relevant references.
    Not uncommon.

    Edit:
    One more rule you can follow, to help you choose, is keep it short, as long is meaningful.
    But I suggest using abbreviations, specially if you not fully familiar with. Can be very confusing in future, when back to the code. You don't need for example putting full proper grammar, in function/var name. So for example ShootGun instead ShootTheGun etc.
     
    Last edited: Dec 26, 2018
    angrypenguin and Ryiah like this.
  6. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    I can not refactor.
    Unity serialization will be a big nightmare, if I use the functions inside inspector (animation events, unity events, event triggers, UIs like buttons.)
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    You use VS to do that.
     
    Ryiah and mahdiii like this.
  8. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    :(
    Always I reach this point and argue about that.
    To be or not to be.
     
  9. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    733
    Always be VSing.
     
    Ryiah likes this.
  10. In my mind the action and the checks for the availability of the action are always go together so I don't try to mirror it in the naming.
    So I vote Shoot() or UseItem(). If (!) I need to check the conditions of an action outside of the action itself, then I refactor it out in a method CanShoot() and/or CanUseItem(). And of course the actions would call into these.
     
  11. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,955
    Deleted User and Antypodish like this.
  12. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    I know about camelCase , PascalCase, etc.
    My problem was about naming and selecting nice names for general big functions that handle many jobs.
     
  13. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,955
    Generally speaking a function shouldn't handle many jobs. It should only handle one job.
     
  14. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    I mean I can call other functions inside one function. I need to name it in one abstract name. It is my problem.
    You are so clever. Sorry. I delete the thread, if it is better.:)
     
  15. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    Knowing respective synonyms, for group of words, can help a lot.
    You can always use word, or web search, to find appropriate synonym.
     
  16. I think that's a very bad idea. You will butcher your own productivity if you need to think and look for the names you applied before because you don't know the words naturally. Although it's a great way to learn new words over time, that's for sure. :D
     
    angrypenguin likes this.
  17. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    If you are young person, or even none native English, and want to use strictly English, I think is good idea to learn. But will work for any language. Eventually you will pick up them naturally.

    Sometimes having simply blackout in mind and can not think about right synonym, at given moment, even knowing it exists and heard it many times.
    So little help, can be useful ;)

    Is learning process like any other.
    For example by asking your colleague, or just like looking into programming tutorials / documents.

    Either way, I would not be expecting spending on it, more than few min at most.
     
    mahdiii likes this.
  18. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,083
    Then you have more to worry about than case usage.
     
  19. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,955
    Antypodish and Peter77 like this.
  20. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    LOL. OK dude. use use use... :D
    I know many synonyms for it.
    apply, utilize, deploy, make use of, employ but use word has only three letters.
    Yes I am not a native.
     
  21. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    I expect a Try method to always return a boolean if it was a success since thats how the .NET api methods named Try works
     
    angrypenguin likes this.
  22. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    The shortest name that clearly convey's the intent. And be consistent.

    So 'TryToUseItem'.

    'To' doesn't convey any new information, I would drop it.

    'Try' has semantic meaning already. (As was just pointed out while typing this). Which doesn't mean you can't make up your own conventions, just have a good reason to do so.
     
    angrypenguin and mahdiii like this.
  23. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    My function returns nothing. Therefore, I should not use "Try" at the beginning of it.
    I intend to distinguish between a player really applies the Item and checking conditions to utilize it.
     
  24. Doesn't really matter, just be consistent, maybe the first time the other developer have to check up on your code to see what is happening, but it's important to do it same way every time, because they expect that. To be consistent far more beneficial than be very good at naming things (which does not mean that you can get away with completely unrelated names of course). But you already spent far more time on this topic than an average developer will spend on your method to find out if you put an if statement in your action to find out if the action is valid or not...
     
    mahdiii likes this.
  25. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,955
    How about "OnGameOver"? Or, if it's not the game ended but a stage or a level, "OnLevelEnded" or "OnStageEnded".
     
    mahdiii likes this.
  26. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    Code (CSharp):
    1. private void OnLevelEnded(GameState state){
    2.     if(state==GameState.Win){
    3.           OnPlayerWon();
    4.     }
    5.     else{
    6.           OnPlayerFailed();
    7.     }
    8. }
    9.  
    10. private void OnPlayerWon(){
    11.     //Do something like play an animations, audios, etc
    12. }
    13. private void OnPlayerFailed(){
    14.     //Do something like play an animations, audios, etc
    15. }
     
  27. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    For this kind of thing I use "RequestWhatever(...)". The "Request" part of the name says that I'm not guaranteeing to the calller that it will happen, it is simply a request which will be acted upon if possible.
     
    mahdiii and Antypodish like this.
  28. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    Here's how Crysis does a weapon class - https://docs.cryengine.com/display/SDKDOC4/Weapon+System#WeaponSystem-WeaponClass

    It uses CanFire.

    Natural Selection 2 has an OnPrimaryAttack function which has a condition check on GetIsPrimaryAttackAllowed. Then it checks if there is ammo, etc. GetIsPrimaryAttackAllowed checks if the player is reloading, or sprinting, etc.

    I've used TryInteract before, but it didn't return a bool as mentioned. The 'Try' communicated that it might not happen, but that means there was no way to see why it might fail from the outside, which was fine in my case. The failures would be because there was nothing to interact with.
     
    Last edited: Dec 27, 2018
  29. afianydina

    afianydina

    Joined:
    Jan 22, 2019
    Posts:
    14
    TryToShoot