Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid having the same name for a class and it's namespace, such as Technology.Technology?

Tags:

I am frequently having naming conflicts between a namespace and a class within that namespace and would like to know a best practice for handling this when it seems to make sense to use these names in the first place, besides adding random prefixes.

It seemed to make sense to have a Models.Technology namespace, with dozens of technology classes (ie. Weapon, Agricultural, Recycling technologies) and some related interfaces in it. I also decided to have a Technology abstract class, in the Technology namespace, from which all technologies derive.

However, this is forcing me to use code like this:

public Technology.Technology research(Technology.Technology tech) {...}

and likewise:

public Building.Building build(int count) {...}

By the way I did not name my namespace Technologies since I use that term elsewhere as a wrapper for a list of available technologies...

like image 363
PRINCESS FLUFF Avatar asked Jul 21 '09 09:07

PRINCESS FLUFF


2 Answers

Without more precise indications on when you encounter this problem it's hard to give you some general advice.

But I'm gonna suppose you have this problem when you have a base class in your namespace, with others classes inheriting from it. If that's the case you could call your class BaseTechnology (base class) or AbstractBuilding (abstract class) or IAnimal (interface). This even give you more clarity since you specify in the name of your type something important about it.

like image 132
Ksempac Avatar answered Oct 07 '22 14:10

Ksempac


Well, whether your name your namespaces like that nsMyNamespace or you name you class cMyClasse. I'm personnaly in favor of naming the namespace, because it's not something you use a lot in code.

You can also use TechnologyBase or AbstractTechnology for naming your main abstract class that conflict with the name of your namespace, which is what i recommend to keep the most logical naming convention.

like image 35
Clement Herreman Avatar answered Oct 07 '22 14:10

Clement Herreman