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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

C# Coding Standards and Best Programming Practices and the power of Unity (Newbie info)

Discussion in 'Scripting' started by Napivo, Feb 9, 2015.

  1. Napivo

    Napivo

    Joined:
    Feb 9, 2015
    Posts:
    39
    Let me start off by telling you I am not complaining about anything, but I don't understand the coding standard for unity yet and want some info. (been coding less than 5 days)

    I come from a pure c# background where coding standards usually are
    private or protected fields --- use lowerCase
    public fields and properties -- Use UpperCase

    As I have never used C# as a scripting language (which I like) I wonder what coding conventions you use here as it seems

    Only use major objects use -- Use UpperCase
    private or protected fields --- use lowerCase
    public fields and properties -- Use lowerCase

    I find this rather confusing for inheritance, not knowing if I call a property or protected field. but I want to conform to the community. (as I need to do to customers) And probably will end up writing generic code for some of you.

    I have never written code for Mono (only for windows using Microsoft C#) but is it as powerful in generics and does it include things like LINQ?

    And then I have one more question about the power of scripting in unity. How powerful is it? I mean compared to writing the code in pure C#?

    I am new to all this, I am a good coder, I wonder how unity would accept things like having a 3d ship accept multiple gun placements and multiple cargo containers thrown together in code from different generic classes or should I build a class for each ship type?

    That should do It for now :)

    Thanks
     
  2. Limyc

    Limyc

    Joined:
    Jan 27, 2013
    Posts:
    29
    I follow C# coding conventions listed on MSDN, but consistency is more important than correctness. Use what makes you productive and happy; there is no right or wrong, just consistent.

    The C# features available in the version of Mono that Unity uses is mostly equivalent to the features found in .Net 3.5, which includes LINQ and generic types (though no covariant/contravarient generics since that's .Net 4).

    There is no difference between Unity's C# and pure C# other than the outdated version of Mono we get to use. :(

    Remember that Unity focuses on a component architecture, so it's not always necessary to throw generics at everything. Sometimes it will just make your code more difficult to change in the future. Try stuff out and learn the architecture. You'll figure out what works and what doesn't.
     
  3. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Unity is doing away with almost all of those 'magic properties' - rigidbody, renderer, etc. These were properties that looked like variables. The only thing they will keep is the transform property. This should make it less confusing from a case perspective. As a general rule, when coding in unity, I also use:

    private or protected fields --- use lowerCase
    public fields and properties -- Use UpperCase

    On a related note, as many are unaware of this when starting with Unity, you can use the tag:
    Code (csharp):
    1. [SerializeField]
    2. private int someName;
    if you need to be able to adjust that variable in the inspector - you do not need to make a variable public to do this.
     
    MD_Reptile and cl9-2 like this.