C# Coding Conventions in Unity

Sdílet
Vložit
  • čas přidán 6. 07. 2022
  • Follow these C# Coding Conventions when learning to program in Unity so that your code is easier to read and understand!
    This is the first "byte-sized" clips extracted and edited from one of my full tutorials. If you enjoyed it and want more like it, be sure to let me know by liking the video or commenting below.
    ACCESS PROJECT FILES & SUPPORT THE CHANNEL:
    💛 / iheartgamedev
    iHeartGameDev Merch:
    💛 www.iheartgamedev.com
    WANT MORE?
    Check out the original tutorial this clip is from here:
    ✅ • How to Program in Unit...
    ✦ Like the vid? Please consider Subscribing!
    bit.ly/2YdIb6j
    SOCIAL:
    ✦ Discord
    / discord
    ✦ Twitter
    / iheartgamedev

Komentáře • 104

  • @iHeartGameDev
    @iHeartGameDev  Před 2 lety +94

    Would you watch a longer video explaining even more coding conventions for programming in unity?

    • @trzrekzii9361
      @trzrekzii9361 Před 2 lety +7

      Definitely

    • @HalcyonVoid
      @HalcyonVoid Před 2 lety

      Yes to all. I dont think there is any downside to having more content of any kind. The bite-sized videos are good for quick reference, and the longer videos are great for in depth explanation. And if anyone is like me, which I am sure I am not alone, I dont always need the deep explination for the basic stuff and want to get to the more complex stuff sooner, so I can refresh with the short videos and get up to speed faster.

    • @mracipayam
      @mracipayam Před 2 lety

      yes/

    • @gamegrade8279
      @gamegrade8279 Před 2 lety

      yup

    • @mihaianca8602
      @mihaianca8602 Před 2 lety

      Absolutely! I need this!!!

  • @keryan1374
    @keryan1374 Před 2 lety +6

    Doesn't matter how advanced you are, you'll always learn something on this channel.
    Thanks for the videos!!

  • @hakanviajando
    @hakanviajando Před 2 lety +2

    Thanks for the video. I loved the command pattern video, I hope there are more design patterns explained in the future.

  • @gg_ru
    @gg_ru Před 2 lety +1

    I like short and informative videos like this one, would be great if you could make more of those!

  • @Choco-sk2gj
    @Choco-sk2gj Před rokem

    Thanks just what I was curious about for Csharp! Really great channel

  • @darkdoom907
    @darkdoom907 Před 2 lety

    Exactly, what I needed, I've been writing a lot of coupling codes, and this was the best help !!!!!💥

  • @howdy_official
    @howdy_official Před 6 měsíci +1

    Fields and properties should be named in lowerCase, AFAIK.
    Build in Unity classes made the same way, for example Time.deltaTime etc.

  • @epiksol_
    @epiksol_ Před 2 lety

    Great job, Nicky!!

  • @theoriginalmakaaka101

    In my own personal projects, I like using the Hungarian notation. I find it helps me differentiate between the different variables on screen.

  • @uwinhong
    @uwinhong Před rokem +2

    short but useful video!!

  • @scoontaquex3388
    @scoontaquex3388 Před 2 lety

    Thanks!

  • @RazerFlexing
    @RazerFlexing Před rokem +1

    Bro u saved me thank u so so so much

  • @charlesonis3484
    @charlesonis3484 Před 2 lety

    So cool!

  • @sir.niklas2090
    @sir.niklas2090 Před 2 lety +3

    I always thought that
    private var: _camelCase
    public var: camelCase | Without underscore.

    • @aleksapetrovic7088
      @aleksapetrovic7088 Před rokem

      I think it should be like that. Lets say you're declaring a variable of type PlayerMovement, if you named the variable the same as the class name, it's not that obvious whether you're accessing the actual class or the reference you have to it.
      // This is more obvious
      public PlayerMovement playerMovement;
      playerMovement.DoSomething();

    • @euden_yt
      @euden_yt Před rokem

      This is also what I use and what Rider recommends for fields that are visible on the Unity inspector.
      And PascalCase for public properties.

  • @umapessoa6051
    @umapessoa6051 Před 2 lety +6

    Nice video, that's something that should be used more often by programmers, it's always a hell getting a Unity freelance due to people ALWAYS missing naming conventions and writing poorly named variables, due to that i could never know if its a public, private or local variable

    • @iHeartGameDev
      @iHeartGameDev  Před 2 lety +1

      Thanks Gabriel! Hopefully by sharing this info, devs are more likely to abide by the standard conventions!

    • @Giga4ever
      @Giga4ever Před 2 lety +1

      Sorry, but that's on you. You should provide the conventions(since they are subjective) and enforce them. There are a bazillion tools that do it automatically. So make passing the tools part of the requirements for the work and don't pick low quality programmers.

  • @quaintgamestudio6149
    @quaintgamestudio6149 Před rokem

    Great 👍🏿

  • @jasperjavillo686
    @jasperjavillo686 Před 2 lety +1

    I remember when I first saw this clip when I first found your channel. This clip forever changed how I coded in Unity and my scripts have never been cleaner. Thank you for making this.

  • @Balakademi
    @Balakademi Před rokem

    Thanks

  • @tPlayerioT
    @tPlayerioT Před 8 měsíci

    sometimes i think its Enums that people uses all uppercase like: PLAYER_WALK, IDLE and honestly it looks more unique to enums and little funny

  • @user-ld2cp1pk5j
    @user-ld2cp1pk5j Před rokem +3

    Use this comment to represent that you are interested in "URP/HDRP Explained" video. Big thanks to author for all videos from this channel ;)

    • @Andrew-tl9gk
      @Andrew-tl9gk Před rokem +1

      No come one, there are so many videos about this topic already...

  • @ElboxD
    @ElboxD Před 2 lety

    Nice vid mate!
    BTW, did you code your outro background video?

    • @iHeartGameDev
      @iHeartGameDev  Před 2 lety +1

      Thanks very much! I made the background in Blender!

  • @mohsenmousavi2313
    @mohsenmousavi2313 Před 2 lety +1

    ♥♥

  • @litemystic
    @litemystic Před 2 lety

    Can you please make a video explaining Unity Timeline

  • @kruth6663
    @kruth6663 Před 9 měsíci +1

    Are methods and functions two differrent concepts in c#? I thought they're the same thing.

  • @Tarodev
    @Tarodev Před 2 lety +1

    Great little video Nicky, I agree with everything you said.

    • @iHeartGameDev
      @iHeartGameDev  Před 2 lety +1

      Thanks buddy! Your short and sweet videos lately had me all inspired!

  • @beninjensin
    @beninjensin Před rokem

    Make a video on how to make ragdoll characters like human fall flat or gangbeast and self balancing them

  • @hellfim
    @hellfim Před rokem

    Was expecting to see a bad coding convention, as in older tutorials you didn't follow them yourself. Yet, a good standard (mostly) one is shown.
    Thanks for the great tutorials and best of luck!

  • @alphaanar2651
    @alphaanar2651 Před 2 lety +3

    I agree with majority of stuff mentioned here, except for this one: "always declare static fields on top". This may be the way "you" write your code, but it is definitely not a standard. A standard is to always put instance fields before static ones. Besides this little false convention, all in all, very decent video.

  • @laniakeadev.2271
    @laniakeadev.2271 Před rokem +1

    In an article on this topic on Unity's github it did not memtion _underscore. I also don't use it because it aggravates readability and makes code ugly.

    • @Andrew-tl9gk
      @Andrew-tl9gk Před rokem

      Within the Unity StarterAssets Third/First-Person Controller the Unity team uses exactly the style as he mentioned here with the underscores. There is also an official article from Unity from 2022 september where they mention the undescores the same way as he does in the video. I guess the underscore is used commonly in C# because there is no "this". So e.g. instead of other languages where you write "this.isRunning = true" it is "isRunning = true" and then underscore makes it more readable "_isRunning = true" so it is clear that its a class variable.

    • @euden_yt
      @euden_yt Před rokem

      It does help to know that the variable will not be accessed by external code.
      It also helps finding the variable faster in intellisense

  • @sergiosoba
    @sergiosoba Před měsícem +1

    And with [SerializeField] private... ???

  • @VEETEEGameStudio
    @VEETEEGameStudio Před 2 lety

    I've watched your state machine tutorial so much that it's already burned into my memory that this video clip is from there😅

  • @bunzy5279
    @bunzy5279 Před 2 lety

    If only it didn't matter! Truth be told C# is much easier than other languages so two rules like this isn't crazy bad.

    • @iHeartGameDev
      @iHeartGameDev  Před 2 lety

      Not bad at all! Hopefully more people abide by the standards!

  • @rusluck6620
    @rusluck6620 Před rokem

    meanwhile i be writing variables like examplevariable

  • @Angel-eg8xm
    @Angel-eg8xm Před rokem

    PLEASE , MAKE A 100 HOURS UDEMY COURSE :/

  • @ZiplawDev
    @ZiplawDev Před 2 lety +3

    I use camel case as well in public and protected methods, and pascal case in properties, to denote that properties may execute some code in the getter or setter, and I only use _ for local variables in a function, when I need to differentiate from a global one

    • @iHeartGameDev
      @iHeartGameDev  Před 2 lety +1

      If it works for you and anyone who reads the code 🤗

    • @ZiplawDev
      @ZiplawDev Před 2 lety

      @@iHeartGameDev i find it does fit quite nicely, the only thing is private variables are indistinguishable from public ones inside their own class, but I'm to lazy to type underscores lmao

    • @alphaanar2651
      @alphaanar2651 Před 2 lety +1

      That is just a sloppy naming convention.

    • @ZiplawDev
      @ZiplawDev Před 2 lety

      @@alphaanar2651 care to elaborate?

    • @alphaanar2651
      @alphaanar2651 Před 2 lety +1

      @@ZiplawDev not to throw shade on you, but, using camelCase for *any* public member and underscore for local variables only shows lack of your experience. Reconsider your conventions. Really if you gave your code for a review, the first thing mentioned would be such bad conventions you are following.

  • @weckar
    @weckar Před 2 lety

    i

  • @jeffmccloud905
    @jeffmccloud905 Před rokem

    I know Unity encourages it unfortunately, but public fields in a class in C# are not recommended. You should use property getters/and setters. Reference: all from the Microsoft docs since 2000... "Fields (C# Programming Guide)" and "C# Coding Conventions" and "Field Design". Public fields are "generally not recommended" and should "used sparingly" and even this: "❌ DO NOT provide instance fields that are public or protected."

  • @MichaelGGarry
    @MichaelGGarry Před rokem

    Hungarian notation. Anything else is inferior.
    Instantly know the scope and type of the variable just by looking at its name.

  • @broganking9830
    @broganking9830 Před 2 lety +2

    ALSO CONSTANTS IN ALL CAPS

    • @alphaanar2651
      @alphaanar2651 Před 2 lety +1

      No, maybe in C and C++, but never in C#. ALL_CAPS is not a convention for anything and it is never mentioned in official pages. Class-level constants are still PascalCase, and local constants declared within a method scope is camelCase.

    • @broganking9830
      @broganking9830 Před 2 lety

      @@alphaanar2651 It should be though. I would fail all code reviews for my Jrs if they don't haha rip

    • @alphaanar2651
      @alphaanar2651 Před 2 lety +1

      @@broganking9830 I mean, you may still use ALL_CAPS (or other peculiar formats), it is not a big must to follow standard conventions in the end. But really this naming style does not fit C#, at all. (I subjectively think that it looks too ugly). Maybe it is time for code refactoring ;)

  • @BGDMusic
    @BGDMusic Před 2 lety

    time to do none of these XD

    • @twillij29
      @twillij29 Před 2 lety +1

      As long as no-one has to read your code lol

    • @BGDMusic
      @BGDMusic Před 2 lety

      @@twillij29 it's not gonna make much of a difference but people are gonna judge me probably

    • @twillij29
      @twillij29 Před 2 lety

      @@BGDMusic No, if people have to read your code then it's good to have some set rules and clarity. I don't know if you have experience going into a project mid-development but it's a nightmare to read code that doesn't have any conventions in place. If it's just you reading it, then it's probably fine.

    • @BGDMusic
      @BGDMusic Před 2 lety

      @@twillij29 it's just upper vs lower and some letters but idc it's whatever
      yeah ok you have a point

    • @BGDMusic
      @BGDMusic Před 2 lety

      distinguishing between different types of stuff is good but conversions only do so much

  • @Chronomatrix
    @Chronomatrix Před 2 lety

    I prefer writing public variables using camelCase, I find it easier to read.

  • @kensteelyhuntergames484

    1:25 trouble in that, what a this is not variables, its fileds

  • @mw3653
    @mw3653 Před 2 lety +2

    Talking about code convention and you didn't put the bracket of the example method on a new line. Awful video. This guy has no idea what he's talking about.

    • @iHeartGameDev
      @iHeartGameDev  Před 2 lety +1

      I have no idea what I'm talking about. Thanks for pointing out the mistake. :)

    • @iHeartGameDev
      @iHeartGameDev  Před 2 lety

      Worth pointing out that majority, if not all, articles that I've read regarding bracket spacing state that it's a preference as long as it's consistent throughout the project: softwareengineering.stackexchange.com/a/2786

    • @Masticater
      @Masticater Před 2 lety

      @@iHeartGameDev 😂I love that you dove into more research because of a joke

    • @iHeartGameDev
      @iHeartGameDev  Před 2 lety

      @@Masticater :) I try really hard not to spread misinformation. Had to be sure I wasn’t 😵‍💫

    • @alphaanar2651
      @alphaanar2651 Před 2 lety +1

      @@iHeartGameDev Nevertheless, the official one by Microsoft states that brackets ought to be placed on a separate line. C# is nothing like other languages where the placement of a bracket does not mater. But to be fair, it surely is not a reason to trash this video.

  • @flaschenzuglp9994
    @flaschenzuglp9994 Před rokem

    Fields are camelCase properties PascalCase. No _
    Is Currently c# meta and in my opinion best to read

    • @Andrew-tl9gk
      @Andrew-tl9gk Před rokem +1

      The underscore means "this." because in C# there is no "this"