Coding Conventions in Unity

Поділитися
Вставка
  • Опубліковано 2 тра 2020
  • Sign up for the Level 2 Game Dev Newsletter: eepurl.com/gGb8eP
    In this video, you'll learn about using coding conventions in Unity to write code that is easier to read and maintain.
    #Unity3D #UnityTutorial #GameDevelopment
    Coding conventions are an important and often overlooked part of creating games in Unity. Any software project, whether it be a game, website, or business application, should have some sort of conventions defined in order to make it easier to read and maintain. In this video, I share some of my favorite Unity coding conventions in an effort to show how valuable it can be to have a well-defined visual language across an entire project.
    👨💻 Join our community: / discord
    ❤️ Support the channel: / infalliblecode
    My Favorite Unity Assets 💯⤵️
    1️⃣ Odin Inspector: assetstore.unity.com/packages...
    2️⃣ Shapes: assetstore.unity.com/packages...
    3️⃣ Easy Save: assetstore.unity.com/packages...
    4️⃣ Dialogue System for Unity: assetstore.unity.com/packages...
    5️⃣ Editor Console Pro: assetstore.unity.com/packages...
    ⚡ Learn more about Unity 3D Plus at prf.hn/click/camref:1100l3e8M/...
    👋 Contact me directly at charles@infalliblecode.com
    * Disclosure: These are affiliate links, which means I'll receive a commission if you use them to make a purchase.

КОМЕНТАРІ • 116

  • @beardordie5308
    @beardordie5308 4 роки тому +52

    Lol "There's three of us now!?" "You and I work together a lot." 😆👍

  • @josh_swanson
    @josh_swanson 4 роки тому +29

    Damn, that skype video quality is next level!

  • @ewwitsantonio
    @ewwitsantonio 4 роки тому +29

    Can you pitch shift Barles' voice down an octave and give him one of those technicolor hats with the propeller on top please thanks

    • @modiddymo
      @modiddymo 4 роки тому +1

      ewwitsantonio he’s waiting to give those to Gnarles when he drops the mob coding episode

  • @IsmaelSerrada
    @IsmaelSerrada 4 роки тому +19

    Changing to Rider IDE was one of the best choices I made as a programmer, saves time, suggest appropriate changes to my code, and let me set up other conventions that may not be the default ones.
    I like these videos, there is always room for improvement when coding in any type of workflow.
    Keep up the good work.

  • @subliminalcastillo2126
    @subliminalcastillo2126 3 роки тому +3

    What I do instead of copy and pasting code, is I retype the code & even modify it as I am going. This way I get a much more intimate experience with each part of the code, and can also understand it better as I'm figuring out how I should break it up, style, & rename all the pieces of it.

  • @fmproductions913
    @fmproductions913 4 роки тому +2

    Important concept to cover to, and I like the brought up reasons on why to do it:
    - Making it easier to read and understand the code (and a well fitting blog post structure analogy)
    - thinking about you as a past self/current self and a future self that might come back to the project in the future while perhaps having forgotten a few things about it already.
    Thanks for the vid!

  • @CreativeSteve69
    @CreativeSteve69 4 роки тому +5

    I have to say Infalilible Code I'm glad that I found your channel back in 2016 when I first got interested how GameDev works. I love how you bring sketch comedy format into something well through educational content. I learn a lot through this method please keep it up. :)

  • @faythe03
    @faythe03 4 роки тому +2

    In the last couple of days I have been reading about the Microsoft coding conventions, so this video came out at the right time for me!

  • @EricKesterson
    @EricKesterson 4 роки тому +4

    As you state in the video having a convention for your code at all is more important than worrying about which convention to use. I wanted to make an argument however for always prepending class member references with "this." (for example a variable declared as private float speed; would be referenced using this.speed. The reason I favor that approach is because "this" is a language feature that can only be interpreted in one way (the way the language interprets it). I've seen many different uses of "_" in unity code and you can find yourself confused when swapping between your own code, unity documentation, and downloaded / copied / tutorial code. Sometimes private members are prepended with "_", sometimes method arguments are prepended with it. I come from Java software development which is where I picked up this standard, however some of the Unity examples also use it (though the unity docs seem to be some of the most inconsistent styled code anywhere). Allowing an IDE such as Rider to provide formatting is also a good argument, however I have not yet switched to Rider myself.
    As for public vs private, personally I haven't actually found it to be extremely important to differentiate between them when reading through code, so I don't bother to differentiate their naming. To be clear, I do carefully consider what members should be public or private, but once that contract is established, I don't usually need to consider it any further, so I don't use a naming convention to keep them visually distinct. Knowing which variable is a member and which is a method argument has proven much more valuable to me.
    These approaches have done well for me as a hobbyist Unity developer for the past 5-ish years and as a software developer for the past 15. As stated in the video this will always be a divisive topic, and I don't intend to detract from valid arguments for other styles, just offer my own experience.

  • @TNTCProject
    @TNTCProject 4 роки тому +2

    Love how you edit your videos. Good job!

  • @Spherous
    @Spherous 4 роки тому +2

    Another great video! Keep it up! I really like your format, by the way, flows well.

  • @subliminalcastillo2126
    @subliminalcastillo2126 3 роки тому +3

    "Wait. There's 3 of us now?"
    HAHAHA. That made me laugh so hard that i have a cramp in my chest now.

  • @PandemoniumGameDev
    @PandemoniumGameDev 4 роки тому +1

    Awesome video as always! 🔥 Would love to get a tutorial on the most common mistakes that kill your game's performance and how they can be avoided.

  • @DeathxStrike18
    @DeathxStrike18 2 роки тому

    I tend to do Public Field > HideInInspector Public Field, Serialized Private Field, Private Field. My logic is the more exposed variables are at the opening of the Script with more secure variables deep within the script. Open > Closed formatting. Think of the script as a box the deeper you dig into the box the harder to get to the items.

  • @shieldgenerator7
    @shieldgenerator7 3 роки тому +1

    In Visual Studio, use CTRL+K, CTRL+D to autoformat the current file.

  • @JewGames
    @JewGames 3 роки тому +3

    Your videos are great, so much dedication and work on each video you do, amazing please keep it up! :)

  • @iercan1234
    @iercan1234 2 роки тому

    watched this in 4K it saved my life

  • @cuttlas2955
    @cuttlas2955 4 роки тому

    I love this channel and how simple it is to underestand thing here ,

  • @stefano4993
    @stefano4993 3 роки тому

    Great video! Totally agree about consistent naming. Just not sure about private and public methods. I try to keep them logically structured, also because of regions....

  • @mrgetrext297
    @mrgetrext297 2 роки тому

    This video formatting is amazing, definitely earned my sub

  • @verified_tinker1818
    @verified_tinker1818 4 роки тому +4

    3:31 I'm going to murder anyone who does this to my code. Nothing triggers me more than braces going below the declaration. It's ugly, inelegant, and almost never makes the code any more readable.
    On a brighter note, what's that font you're using? It looks rad!

    • @nooftube2541
      @nooftube2541 3 роки тому +2

      Looks like you aren't from c# world :)

  • @saito853
    @saito853 3 роки тому +1

    Working with a friend on a project for my thesis, his naming convention SUCKS DICK and I'm sending this to him.
    Thanks for the great video Charles (and Barles)

  • @imxron
    @imxron 4 роки тому +9

    The amount of time I've spent convincing teams I've worked on to use auto formatters / linters is almost as long as coming up with variable names. Ctrl + Alt + L 😉.

  • @risulkarim5781
    @risulkarim5781 4 роки тому

    Hey Charles, great stuff once again.. you guys are being my favorite couple of mentors :-D Just curious about which font you use? It looks so cool

  • @hansshadow360
    @hansshadow360 4 роки тому +3

    conventions begin at 3:20

  • @lvx969
    @lvx969 4 роки тому

    Hold Control and press the E Key and then the D key to auto format spacing and tabbing in Visual Studio Community 2019.

  • @Redheadtama1
    @Redheadtama1 4 роки тому +1

    Hi Charles. Thanks again for continuing to teach best practices! You always give accurate and helpful info. If you don't mind, could I offer a little bit of constructive feedback? Please don't take it the wrong way. I honestly just want to help but feel free to ignore me if you disagree with my comments :). OK here goes:
    Personally I find the structure of these videos can distract a bit from the information you're providing. It can come across a little bit cringe and feels like it takes twice as long to put your point across. I say this because sometimes I find myself re-watching one of your videos to remind myself of something you said, but it's really difficult to find what I need because I have to filter through all this exposition and conversation between you and yourself.
    Again, thanks for your work on these videos. I honestly do appreciate it. Also I purchased Enhanced Hierarchy recently and it's been great so I will be sure to leave you a positive review on the asset store :).

  • @sebastianbryant4216
    @sebastianbryant4216 2 роки тому +1

    Great video as always!

  • @sparrowhawk81
    @sparrowhawk81 4 роки тому +1

    What are your thoughts on how to handle modifying an asset you got off the Unity Asset Store? I haven't really come across the need to do this yet personally, but I know you are allowed to modify the source code of assets (the ones that aren't DLLs I mean) you get off the asset store for your own purposes, but I could see myself maybe wanting to rework something someday to add features to it or whatever. Obviously you could just dive in and go for it but have you come across this yourself? Any tricks with inheritance or anything that are useful? Wrapper classes?

    • @tylocook
      @tylocook 4 роки тому

      I came here to make a comment that you should not edit the source code of a big asset you import into your project just to style its code to look like yours.
      The example he used was a snippet of code from Stack Overflow which should not be confused with using somebody's hundreds of lines of code.
      Maybe Charles disagrees, but I can see why you're confused.

    • @StefanLopuszanski
      @StefanLopuszanski 4 роки тому

      I modify them when needed, but try to avoid it because if something updates and fixes some bug I have to go through it manually.

  • @artcadedev
    @artcadedev Рік тому

    By the time I saw the thumbnail you had already earned a sub

  • @ujjwalgupta1204
    @ujjwalgupta1204 4 роки тому +1

    Nice Video

  • @Tarodev
    @Tarodev 2 роки тому +1

    I'm working on a conventions video (might be a little more passive aggressive than this video...) and I loved your concept of there always being at least two people working on the project. Do you mind if I quote you with attribution on my video?

  • @veeaxis3892
    @veeaxis3892 3 роки тому

    Is there any naming convention for delegates and events?

  • @albertoravasio849
    @albertoravasio849 4 роки тому +1

    Which visual studio do u use? It's different form the default one

    • @yuan3234
      @yuan3234 4 роки тому +2

      Its Rider IDE

  • @Matthew-zs8nm
    @Matthew-zs8nm 2 роки тому

    Bro, you're hilarious. This was also a great video for a beginner like myself. Subscribed.

  • @UsatiyNyan
    @UsatiyNyan 4 роки тому +4

    As a C++ dev I feel personally attacked by first brace not being on the same line as a class/function declaration

    • @nooftube2541
      @nooftube2541 3 роки тому

      Dat c++ guys come to c# place their braces on same lines and leave... and c# guys have to leave with it...

  • @Abken.
    @Abken. 4 роки тому +6

    Really good video. 👍
    I always fight already lost matches with people on Reddit who hate conventions and they always dislike me when I say that public fields are bad. Everyone starts crying how they are not bad if they're used well. What's the problem with writing good code from the beginning. If I mention SOLID I instantly get 20 dislikes :D But reddit guys hate even properties.

    • @agnichatian
      @agnichatian 3 роки тому +3

      Yeah, many people hate discipline.

    • @nooftube2541
      @nooftube2541 3 роки тому

      Public fields doesn't related with SOLID anyhow. Actually no matter you use public field or property with public setter even from the point of encapsulation view, because both provides same API. The only problem with field that it is not obvious that it should or should not be changed from the outside, but a blind changing it to property won't make your class encapsulated.

    • @Abken.
      @Abken. 3 роки тому +1

      @@nooftube2541 This is true, but a wrong mentality. I'm so tired of this topic. My idea is that when you are creating a new class, you should make anything { get; private set; } if you don't need validation or another logic at this point. Then you realize that you need to expose a setter. There is no difference if you make the exposure through a method or through the setter, it's still an exposure. But at least you will think about "why do I need to expose this property, are there no other solutions to this problem, etc". This is way better than making everything public field and then going back to encapsulate only what you need, because you will ignore/skip unintentionally many cases where you can avoid data exposure. If you are a newbie C# dev, it will be much easier to just write every field as a property with private set and it will be much easier to understand why do you need to hide the unneeded data. That's why in universities the OOP topic is so important that the exams are literally "what's encapsulation, what's polymorphism, what's inheritance" instead of writing some code and solving real problems. It's important that the new devs will know why these things matter. Also I've never mentioned that public fields have anything related with SOLID, but if newcomers don't understand what's encapsulation and why it is important, how can we expect that they will follow SOLID on their desk job as web dev, game dev, etc.? And believe me, if you go to Ubisoft's Unity interview (they need Unity devs right now for a 3D game), they will ask you about OOP, SOLID and not ECS. If you look for ECS jobs - yes, they don't ask for such skills, because they have use almost no inheritance at all. ECS vs OOP is another topic. But yeah, it's easier to start from {get; private set;} and learn why it is needed. The most important thing that OOP teaches is discipline - it makes you write good readable code because not everyone works on its own project alone.

    • @nooftube2541
      @nooftube2541 3 роки тому +2

      @@Abken. though I suggest never write public get property at first. Use readonly private fields, and only when you realize it is needed to be exposed, convert it to public property.

    • @Abken.
      @Abken. 3 роки тому +1

      @@nooftube2541 Yeah, yeah. I meant that if I need to expose data my workflow is - create a property without set, then add private set, etc. Of course that the other data that we don't need exposed should be private fields. And readonly is a good advice that many people that come from Typescript or C++ to C# do, but most of the C# devs don't do. But it's a good advice anyways.

  • @RobLang
    @RobLang 4 роки тому +12

    Complaining about m_ while still doing _.
    IDEs can colour variables by scope so you don't have to do that.
    Otherwise, love the video, great job.

    • @lucasmontec
      @lucasmontec 4 роки тому +2

      You can choose a convention with your team for your project that is close enough to the c# one but still more up-to-date. Your code base just have to be consistent with itself and across time.

    • @RobLang
      @RobLang 4 роки тому +3

      @@lucasmontec I completely agree that it's the choice of every team. In my team, it's my choice! 😀 The underscore is a hangover of Hungarian notation, which was phased out when IDEs became more interactive. 👍

    • @lucasmontec
      @lucasmontec 4 роки тому +3

      @@RobLang I have banned the use of underscore im every team I have worked with. We are in 2020, there is absolutely no reason to use it, just makes code ugly and hard to read.

    • @RobLang
      @RobLang 4 роки тому +2

      @@lucasmontec lol! Perhaps we should start a revolution?

    • @tylocook
      @tylocook 4 роки тому +4

      I like to use m_ or _ just so I can type those first characters and have intellisense show me all the member variables. I use it to categorize class level variables

  • @TheLucausi
    @TheLucausi 4 роки тому +3

    Where is the Civil War? Camel vs Pascal? just kidding. Your video is awesome, great coding conventions! ( Don't agree too much with underscore on attributes fields, but i love u anyway xD )

  • @neitron29
    @neitron29 4 роки тому +8

    Personally, I hate Upper case for public members, because Unity`s libraries are written with lower case rule. Why Would you prefer to mess up with both?

    • @jorhoto7064
      @jorhoto7064 4 роки тому +3

      could be a way to distinguish between yours and Unity ones.

    • @nooftube2541
      @nooftube2541 3 роки тому

      But you still using system lib, so it would be messed up anyway. Dat unity developers.....

  • @ujjwalgupta1204
    @ujjwalgupta1204 4 роки тому +1

    new to the channel

  • @jorhoto7064
    @jorhoto7064 4 роки тому

    According to conventions should we use the prefix: _ for all our private fields? Option 1 or 2? E.g.:
    1. private health
    2. private _health

    • @EricKesterson
      @EricKesterson 4 роки тому +2

      personally, I always declare as private int health; and then when referencing I use this.health. My argument for using "this.health" is that it is the ONLY coding style that is also a language feature which CANNOT be interpreted another way. I've seen "_" used in many different ways, so if you jump between projects or copy some code you could easily become confused by its use.

    • @nooftube2541
      @nooftube2541 3 роки тому +1

      As you prefer.

    • @halivudestevez2
      @halivudestevez2 Рік тому

      I hated underscore for a long time ( used "this"). Now I started using it. Seems professional, and one more difference against the local variables in the method.
      _ health - class member private field
      health - local variable ....
      this._health - overwhelmed signature

  • @travelsizedlions
    @travelsizedlions 4 роки тому

    You mean the bracket thing isn't the most polarizing?

  • @victrolaface
    @victrolaface 4 роки тому +2

    Charles in charge, of my life, of my youtube.

  • @benoitrousseau4137
    @benoitrousseau4137 4 роки тому +1

    Amusingly, Unity is at odds at a few places with the official C# style guidelines from Microsoft. For example, public fields should be pascal cased in C#. (ie Time.DeltaTime, not Time.deltaTime.) That said, I would use the Unity style when working in Unity.

  • @RayHorn5128088056
    @RayHorn5128088056 Рік тому

    Why can't I use whatever code I want to write so long as it works?!?

  • @ImranDaybyDay
    @ImranDaybyDay 4 роки тому

    Charles is like Clark Kent

  • @StefanLopuszanski
    @StefanLopuszanski 4 роки тому +1

    No mention of where to put the { }? :-P

  • @Faygris
    @Faygris 4 роки тому +3

    I only use Hungarian pascal snake case.
    *private string M_Str_Prove_Me_Wrong;*

  • @Dfjs427
    @Dfjs427 4 роки тому +2

    Oh thank god you mentioned { on the same line. It is so disgusting that I have to put everything on hold until it's sorted.

  • @daniilparokonnyy7980
    @daniilparokonnyy7980 4 роки тому

    Should you use "_" or not - that is the question!

  • @Bossti
    @Bossti 4 роки тому +1

    Opening brackets on its own line make me sick ^^"

    • @Unavailable8923
      @Unavailable8923 2 роки тому +1

      Same here. It just pushes code off the screen by wasting vertical space.

  • @tomerssh
    @tomerssh 4 роки тому

    6:37 my engineering career in a nutshell

  • @FuzzyDPozzy
    @FuzzyDPozzy 4 роки тому

    use underscore for private fields such as private float_number;

  • @alonsorobots
    @alonsorobots 4 роки тому

    you just snuck in the underscore before the variable without mentioning it!

  • @Patrick_Bard
    @Patrick_Bard 2 роки тому

    Coming from a cooperate Java background, curly brackets on their own line is the only rule I can't stand.

  • @doge9203
    @doge9203 3 роки тому +1

    ah not the brackets

  • @monkeyrobotsinc.9875
    @monkeyrobotsinc.9875 3 роки тому

    after speeding up ur vids to 1.25, i know that u slow them down before uploading them to youtube.

  • @jorhoto7064
    @jorhoto7064 4 роки тому +1

    What if I have a class Health and a property Health?
    Should we name it the same?
    public class Health
    {
    public int Health {get; set;}
    }

    • @divingradish
      @divingradish 4 роки тому +3

      I would do this:
      public class Health
      {
      public int Value {get; set;}
      }

    • @nooftube2541
      @nooftube2541 3 роки тому

      You can't do it. It won't compile.

  • @YuvrajRaghuvanshiS
    @YuvrajRaghuvanshiS 3 роки тому +1

    Curly braces on next line.... Noooooo

  • @user-gs2rh1ch7e
    @user-gs2rh1ch7e 4 роки тому

    I hate fucking skype tutor format so much but your videos are too damn informative

  • @ionthruster9572
    @ionthruster9572 4 роки тому +1

    recommend vs+Reshaper

    • @nooftube2541
      @nooftube2541 3 роки тому

      Rider - x64, vs - x86 (and will never be x64). Checkmate.

  • @enriquellerena4779
    @enriquellerena4779 2 роки тому

    m_ I remember that, what a pain. Xd

  • @jorhoto7064
    @jorhoto7064 4 роки тому +4

    Hi, did anyone notice the 2 guys in the video are very similar?

  • @DoubleM55
    @DoubleM55 3 роки тому

    I find it ugly having private fields start with underscore. Any IDE will not let you use private fields outside class, so I don't even see the point. If it was really important then compiler should check it, but it's redundant and pointless.

  • @andredias5061
    @andredias5061 3 роки тому

    my brains hurts... who am I? is there a life worth living? what's life? Is Life alive by itself?

  • @ApacheGamingUK
    @ApacheGamingUK 3 роки тому

    Why does Unity force people to break the C# Coding Standards? Intellisense is a total mess, and makes it so easy to break encapsulation.

  • @unlockener
    @unlockener 4 роки тому +2

    Unity properties vs c# properties. Why bring more chaos to this world

  • @googleuser4720
    @googleuser4720 4 роки тому

    Ha ameteur!