Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How should I use camel case?

Discussion in 'Scripting' started by nameisinprocess, Feb 8, 2023.

  1. nameisinprocess

    nameisinprocess

    Joined:
    Jan 4, 2023
    Posts:
    6
    I've been learning Lua for the entirety of 2022 and now I'm transitioning into C#, but I've never actually learned how camel case is supposed to be used. I just use camel case when I feel like it. Or am I overthinking this please educate me.
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,380
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    As a software engineer, professional or amateur, it is definitely worthwhile to at least make some effort towards making your code somewhat compliant with C# standards.

    The first issue is that 10 different programmers will have 10 different ideas on what is critical / irrelevant and how to do various of the details.

    The second issue is that regardless of how a piece of code is written, you MUST be able to understand and work with it just as if it was correctly formatted.

    And finally, you WILL encounter code (your own, or teammates, or your boss' code, or a third party library) that is poorly formatted or written counter to what you expect. Again, you MUST be able to work with it.

    Disclaimer: I am in no way a staunch adherent to code style. My code style technically drifts away from strict C# styles all the time. But I will refactor code anytime I realize that I have done something unclear. Clarity and correctness of function are my primary goals, not blind adherence to style guides.
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,925
    Yes, you're overthinking it. The compiler doesn't care, and there's nothing in C# where
    cat_count
    or
    catcount
    will cause a problem and you realize "oh, that's why I should use
    catCount
    ". I've been using things like
    MAX_WIDTH
    and
    _moveHelper()
    and even
    bool is_aCow()
    and never had a problem.

    Most game designers are probably like you -- they used another language first, think of C# as a funny version of that, and have no interest in doing things the C# way. You're style is probably already better than most people's -- the LUA style guide seems better written and more complete than official C# ones. And it pretty much says what you said -- use camel case whenever you feel like it. For example getNearestFoe seems to want camel-casing, but getJSONheader feels better than camelized getJsonHeader.
     
    Kurt-Dekker likes this.
  5. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,135
    In my own projects I use camelCase for all fields, local variables and parameters. I believe this is what Microsoft also uses internally.

    Unity's source code also uses camelCase for properties.

    At my day job we use camelCase for all private fields and properties and PascalCase for all public fields and properties.


    In my opinion the main thing for keeping your code base nice and tidy is consistency. Come up with some rules, codify them in your IDE settings, and considering performing automatic formatting for example whenever you save a document.
    Options.png
     
    Last edited: Feb 8, 2023
    TzuriTeshuba likes this.
  6. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    I agree with Kurt. Whatever makes it the most clear and organized for you is what you should use. So long as you and others can clearly understand and work with it is all that matters. I've been coding since 1997 and know or have known over 30 languages and my style continually evolves over the years. I use camel case for everything because it's clear to read at a quick glance and anything different feels like a pimple on a clean face. The one thing I see other people do that really irks me is what I call crunchy code, where they put no spaces between anything unless it's required to make it function. I call it crunchy because it's all crunched together. A surprisingly large number of people do that and it's almost unreadable in that format. Just try to make it easily readable by both yourself and others you share it with and you're doing it right.
     
  7. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    437
    In 2023, I'm still using Hungarian notation because my employer requires it to keep the project consistent.

    Choosing a coding convention isn't as important as neat and readable code, but that comes with experience.

    Beginner programmers often like to overthink things. I was like that myself, and it often paralyzed my progress. Example? Wasting the whole day thinking about how to name the class... And when one appeared and I typed it from the keyboard, it looked ugly, eg IllegalInstitution. You can get obsessions.
     
  8. nameisinprocess

    nameisinprocess

    Joined:
    Jan 4, 2023
    Posts:
    6
    These are all great suggestions, but personally I think I'm going to just work at McDonalds thanks though xoxo.
     
  9. nameisinprocess

    nameisinprocess

    Joined:
    Jan 4, 2023
    Posts:
    6
    I think I'm going to go with Unity's standards for naming conventions.
     
  10. nameisinprocess

    nameisinprocess

    Joined:
    Jan 4, 2023
    Posts:
    6
    If you hate crunchy code, look at free assets in Roblox Studio and you'll instantly explode when you see the scripts.