The unity documentation recommend to use namespaces to organize your
code, avoid extended class name, and make more maintainable code.
Yes !
ControlerCameraAction
becomes Controleurs.Cameras.Action
and
CameraAction
becomes Cameras.Action
.
Yes, and no.
The idea behind namespaces is organization not just simple use this feature called namespaces
. And that organization should follow a logic. If every class has a different namespace then you will have to import as many namespaces as classes that you will use in your code.
You can think in terms of 'modules' or maybe see if a layered architecture can be useful for you.
If you have ControllerCameraAction
and CameraAction
, you can a.- use the namespace Cameras
for both (then you will have Cameras.CameraAction
and Cameras.ControllerCameraAction
) , b.- if you have a layered architecture (like MVP, MVVM, or some more DomainDesign Driven, etc.) you can have namespaces using the layer name and the module. (then you will have something like Presentation.Cameras.ControllerCameraAction
, Domain.Cameras.CameraAction
and this can help you to follow an Onion architecture).
The syntax for
namespaces are like this:
namespace Domain.Cameras
{
public class CameraAction
{
}
}
And you use them with using
directive
using Domain.Cameras;
namespace Presentation.Cameras
{
public class ControllerCameraAction
{
private CameraAction cameraAction;
...
}
}
More about namespaces here!
By default all classes that don't have an explicit namespace belong to global
namespace, so even when you are not writing any namespace you are using one.
Unity will not make any difference between namespaces, this is more a c# characteristic. And it helps with organization, separation of concerns principle, and avoiding name conflicts too, but in the last instance, your class names should still be representative and clear enough to understand what that class does. If you see
Camera.cs
and
Camera.cs
it's really hard to see what class does what. If you open those files and see the namespace/code/folder where they are that will help, but the idea is save those extra seconds/cognitive load and just be more explicit with your names.
As a complement here you can see another interesting discussion about namespaces use.