|
As you know, in my spare (hah!) time I have been dabbling in Cocoa and Objective-C. Regardless of the outcome of this experimentation, I am finding it an extremely enlightening process. As a rule, many developers tend to stick to one language, one environment, and avoid moving away from it. It's a natural tendency for people to enjoy staying in their "safe zones". Me, I like to identify my safe zone, then identify a zone in which I have no experience whatsoever, then dive in head-first like a kid jumping into a pile of freshly raked fall leaves. When you go back to your safe zone after the jump, you might have a leaf or two stuck in your socks. Think of those as really useful nuggets of information! OK OK so the analagy is a little thin, but I sure as hell did love jumping in leaf piles when I was a kid!!
Anywho, I've seen comments from people on this blog and in other posts elsehwere that indicate that, as a Cocoa developer, you really should be prefixing your own class names with some kind of two-letter abbreviation. For example, if you write code for a company called SuperSoft (apologies if there really is a company called that...), you might create classes like SSPerson or SSDataElementDooDad or SSThingamawhatsit.
I've been a self-professed C# addict since the first bits of the language were submitted to the standards bodies for approval. One of the first things that I grew to love about working in .NET when I first learned the environment was namespaces. See, namespaces are basically naming containers. The goal of a namespace is to provide contextual difference between a class named Person in my application, a class named Person in the core libraries, and a class named Person in someone else's application. While the likelihood of name collisions of applications in unrelated domains is still pretty low, the elegance of being able to refer to my Person class as DotNetAddict.Libraries.Person from the "outside" and simply Person from within my own namespace is quite compelling and powerful.
Which one do you prefer? SSThingamawhatsit or SuperSoft.Thingamawhatsit. I've become so entrenched in my C# safe zone that I find the two-letter prefix appears to me as a cheap hack, while the namespace appears to me as an elegant solution to a real-world problem.
I realize that Objective-C is, at its core, somewhat lower level than C# (Objective-C is, after all, "real" C with object-oriented underpants sewn on), and a lot of people feel that the intrusion of namespaces onto Objective-C might muddy the waters and blur the original clear intent that created the language. Me, I think if you made namespaces optional (for instance, allowed a null or nonexistant namespace, e.g. the namespace in which all code exists today) and could bolt it on afterward, that would be fantastic.
For example, what if I could do this to my Person.h file:
@namespace KevinsSamples.LeopardTesting
... previous .h file ...
@end
How cool would that be? Then I could either declare instances of the Person class, or instances of the KevinsSamples.LeopardTesting.Person class. However, since the dot notation is currently being used for the Leopard property syntax, maybe it could be KevinsSamples::LeopardTesting::Person, like the C++ syntax.
Anyway, my wish list is now widely known. I expect Objective-C namespaces under the tree for Christmas this year :) (and maybe an HD-DVD player...that'd be sweeeet)
This one comes up every second week or so. Namespaces would indeed be a
minor convenience, and useful for cases like loading plug ins. I myself
have suggested a low-impact mechanism for implementing them (
http://lists.apple.com/archives/objc-language/2007/Jan/msg00023.html ). The
problem, though, is that class name conflicts are rare and obvious when
they happen, but in a dynamic language like ObjC method conflicts are a
real problem, since superclasses or categories can effectively add methods
at any time. The results of such conflicts tend to be subtle and lead to
the tearing-out of much hair. Defining an elegant mechanism that avoids
this while still allowing you to use a version of a method that you didn’t
know about at build time, without completely redesigning the language
(serious runtime changes would be a given) appears to be an unsolved
problem.
Quite true. The balancing act between keeping ObjC as dynamic as it is
right now (being able to send messages to objects at runtime without
knowing at design-time if such a message can be passed, as well as
dynamically changing responder chains at runtime) while adding the naming
security of namespaces is a tough one. Sure, namespaces would be nice - but
only if they can be had without harming ObjC as it exists today.
Kevin, I've been enjoying this series of posts quite thoroughly. I'm a C#
junky too, but have been quite tempted by your posts to try Objective C 2.0
as well. Alas, it will have to wait until after I master Lisp and Haskell
. . . In any case, I wanted to chime in on my belief in the importance of
namespaces. I'm a big believer in DDD and just wanted to note that Evans
makes a point that namespaces can be very helpful in making the domain
model much more "explicit" (aside from just preventing naming conflicts).
Totally agree. Namespaces are about readability, explicitness, and
clean-looking code. It's a handy side-effect that they can be used for
naming conflicts as well. I'm hoping that Apple will eventually find some
way to deal with the method name lookup issues present in namespaces
w/Objective-C and give that to us as an option. For example, Garbage
Collection is going to be an optional compiler flag... It'd be nice if I
could also optionally compile my app to use namespaces.
On the other hand, it can be a little confusing if the class has the same
name as one in another namespace and it's not clear which it is. That isn't
a problem when the distinction is in the classname itself.
Another thought. Objective-C gets some abuse because of the verbosity of
the method names, but Java can be quite annoying with the length of the
fully-qualified class names. At least SSTableView is concise... ;^) Even
JonHendryTableView is concise compared to
com.sun.java.foo.bar.grids.TableView.
I hear you on the "T" prefix from Delphi... I hated that :)
I love the verbosity of invoking methods in Objective-C, its extremely easy
to read.
Namespaces are a lot like something else you won't see in ObjC: operator
overloading. While in *theory* it sounds like a good idea, practices in
the real world seems to conspire against it. Just as I don't like to see a
bit of code like "a + b" and have to go searching to see if that
accomplishes addition or string concatenation (or maybe even something more
esoteric), I don't like to see "Person" and constantly concern myself about
what what of many possible classes it refers to.
Also, frameworks can be used to group classes in such a way that it
accomplishes what namespaces do. For example, even though NS is supposed
to be NextStep pseudo-namespace, you could replace them all without
changing your code simply by changing the Cocoa include statement to that
of a framework of your own design with the same classes. I believe that is
essentially how GNUstep works behind the scenes.
When I was first introduced to ObjC&Cocoa, I suddenly realized that they
had no name spaces.
Coming from academic Java at that time, I was stumbled that this system
could exist without it. My "instructor" assured me that there never have
been problems with everone following NeXT with a class name prefix (as you
mentioned above).
That's an excellent point... Namespaces would solve a non-problem in Cocoa.
Very well put.
In my experience the main problem with the prefixes is coming up with one
that is aesthetically pleasing.
I'm a c++ developer and I appreciate the extra flexibility in design
allowed by namespaces in c++. It would be nice to have it in objc if it is
technically feasible.
Not only does the verbosity of ObjC comments make them easier to read, so
does the inline-argument thing... - might look in some other language like
MyClass.foo(a,b,c), with only useful variable names to guide us.
Long time listener, first time caller. And as a Mac user, I have to say,
"Joooiiiiiiin usss." Kidding aside, namespaces might be nice, but it'd be
something to thoroughly think through.
This is very after the fact, but...