Search Unity

Coding with namespaces in large projects

Discussion in 'Scripting' started by MapuHoB, Oct 2, 2015.

  1. MapuHoB

    MapuHoB

    Joined:
    Aug 17, 2014
    Posts:
    50
    I just downloaded Shadowgun from the asset store and noticed that the project isn't using any namespaces. And in my opinion the project is quite big and as far as I knew before, large projects use namespaces. I myself in my tiny projects started using namespaces about a few months ago and now started wondering if there could be any cons for using them? Are there any? Do big projects or any projects in general use namespaces? Why would such a big project like Shadowgun not use namespaces?
     
  2. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    For what I know, namespaces are just great for organization. If the dev(s) of Shadowgun think that namespaces aren't necessary, I guess they'd just skip it (like I do). I, personally, just use classes instead for easier reachability. There may be cons too, but I am not sure. I just know that they're not literally necessary whether the project is big or just a small mobile game.
     
  3. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Namespaces are great for organization and decrease the sensitivity for errors across the board ( a class that should not be reachable for another class, can't be reached if it is in a namespace that is not added to the usings, thus can't be accidentally called) and also allows the use of classes with the same name (f.e. System.Random and UnityEngine.Random).

    Personally I always use namespaces and have created my own Visual Studio class template for Unity which includes them by default (and also includes most used regions).
     
  4. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    A namespace must always be "using NameSpaceName"... etc right?
     
  5. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Yeah, just like unityengine is added to classes with the call
    Code (CSharp):
    1. using UnityEngine;
     
    Sykoo likes this.
  6. BackwoodsGaming

    BackwoodsGaming

    Joined:
    Jan 2, 2014
    Posts:
    2,229
    For a complete project, I would venture to say namespaces would be nice but not necessarily a requirement. However for assets that are built to include within a project, I wish they would all use namespaces. You never know what other assets the developer is going to be using and if someone happens to install two assets that happen to use the same class name... Lets just say it causes a mess that using namespaces could have prevented.. I'm like @Timelog. I use namespaces for everything. Better safe than sorry and if I'm using a namespace, there is no question in which method's I'm actually accessing.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    FWIW, in the Unity community, namespaces are a relatively recent trend - it wasn't until late in 4.x that Unity's first party code really started using them, and that's when they caught on. It looks like Shadowgun was released in 2012, long before then.

    So if the question is "Why doesn't Shadowgun use namespaces?", the answer is probably "Because when it was written, almost no one was using them."
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    I'm sure they would have if they could. But Unity didn't support namespaces for MonoBehaviours back then, so no one could use them.
     
    Kiwasi, Dantus and ThermalFusion like this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This is the key point. For a long time MonoBehaviours had to be in the top level name space. Which means that namespaces were pretty much useless in the Unity context.

    That has changed now. So namespacing everything should be the default for new projects and new assets for the store.
     
    GibTreaty and Timelog like this.
  10. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    I personally don't really use them, although I probably should. The only use case I see is in the case where I want to download an asset that may have classes with the same name as my own classes.

    I rarely ever use assets from the asset store (I have one antialiasing asset only, I believe) but you never know.
     
    GibTreaty likes this.
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I tend to use them to control dependencies. I'm a chemical engineer by trade. I like the concept of well defined unit operations with only narrow interactions between them. NameSpaces help me achieve this.
     
    Timelog likes this.
  12. MapuHoB

    MapuHoB

    Joined:
    Aug 17, 2014
    Posts:
    50
    So basically I have been on the right path, using namespaces? Thank you all! :)))
     
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    For really big projects, it's often worth splitting it into separate assemblies. ;)