Update (2009/06/02): It turns out some of my understanding about this issue was wrong. I've never been a Flex guy. I will keep this post as is below. My current understanding is that "promoting components to the global namespace" was not the goal, but only a side effect. Albeit a very important one IMO. So, this post still has a point.
Without any rant on (custom) namespaces, here's why I think using prefixes would have been the right decision, and probably the only viable choice.
First of all, using prefixes (or suffixes or any other naming convention) is not too different than using a namespace.
With prefixes, you are creating a namespace yourself without the compiler knowing about it. With namespaces, compiler knows about this, so it can provide some minor benefits in return (like access restrictions, or some easy syntax).
That's all the difference there is, other than of course you may think namespaces are neater.
Many file systems solve name collision issues using folders/directories. Folders (read namespaces) are helpful for organizational purposes too. You may prefer all your files in a single folder, or you may create folder in folders in folders until each final folder contains a single file. That usage is not related to solving name collision problems.
You don't usually think about namespaces when using folders, like "Now, I'm copying this file into this folders namespace", you just "copy the file into a folder". In the real world, we name different things the same all the time, and we solve name collision issues that follow without thinking about namespaces.
In nearly all computer languages, there are namespaces that the compiler or interpreter deals with whether you are aware of it or not.
So, if namespaces are already in the language, when can a prefix be a better solution? Is this just a personal preference (ignoring minor benefits each provide)?
Let me first state again that there are no problems with using prefixes that would be a disadvantage for long term use, that namespaces don't have, as far as name collisions are concerned, because they are not that different. (If there comes a time you feel you need to change the prefix like to Fx1 or to Fx2, that's also exactly when you would be changing the namespace if you used namespaces).
The Right Question
I'm not a Flex guy. As I see it, on the surface, the question asked is/was:
"There may be a name collision issue, how should it be fixed? Using prefixes or namespaces?"
And the natural answer is "Why not use namespaces? Their main use is avoiding name collision issues and they are there in the language".
But why was there a possibility of name collisions, in the first place?
You don't have name collision issues everyday, because usually the languages provide default namespaces using the file system structure, this handles them well and transparently. AS3 and similar languages use the whole path from the root, so you cannot have files in arbitrary folders. In many languages, only file name is used. In that case, you can put your files in arbitrary folders as you like, but you cannot have two files with the same name in the same project.
So the question asked as "Namespaces or Prefixes?" needs more context, to get the right answer rather than a generic one.
Components were being promoted to the global - root - top namespace, where you wouldn't have to state the namespace at all. Identifiers in that namespace are first class citizens and this was a worthy goal.
When I had an Amstrad CPC464 as a kid (and a VIC20 before that), some of my friends had Spectrum 48Ks, some had Commodore 64's. To us, the computer was the keyboard. And, well, it was.
When I first heard about C, from a friend, he said: "Everything in C are functions, so cool. The language does not have any features, you use external libraries for everything". For us, at the time, a language was something that came with a standard library built into that language, just like having the computer built-in in a keyboard case.
Most of the time languages do have standard libraries, or certain built-in objects etc. You may see the difference, but you may also think of them as a part of the whole language experience, as a part of the language.
Functions you use without using a namespace, can be seen as a part of the language itself. This is not too wrong, with every class or function you write, you are extending the language (in a sense).
So, some class being promoted to the special, top namespace is something important. This is like not like moving a class from this namespace to that other ordinary namespace...
- GOAL: Promote these classes to the top, global namespace.
- OK, but what if we have a name collision issue there.
- Prefixing should solve this 99%.
- OK, but we have namespaces to solve name collision issues. They will also solve the issue 99%, and they are neater.
- Sure, but what was the GOAL again?
You see, if your goal is promoting some names to the special top namespace, you cannot solve any name collision issues you anticipate, by putting them in another namespace.
So, the question is not about prefixes vs. namespaces. Using another namespace is not a viable option for solving name collision issues in this case.
The correct question is: "Should certain components be promoted to the top/global namespace?". Using namespaces is just giving up on the idea. It's a defeat. Assuming, you want to do the change, because you don't like using prefixes, and because you care about name collision issues, you just abandon the feature.
You may argue that top/global namespace should be kept clean. I have a lot to say on that too, but in another post, if I can find the time.
Recent Comments