The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








Latest Diggs - Programming

Internet Blogs - Blog Top Sites

Site Hits

Total: 2,639,104
since: 19 Jan 2005

Does Objective-C need Namespaces?

posted Wed 28 Mar 07

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)

tags:                  

links: digg this    del.icio.us    technorati    reddit




1. Ahruman left...
Wed 28 Mar 07 10:36 am

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.


2. Kevin Hoffman left...
Wed 28 Mar 07 11:29 am

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.


3. Rob left...

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).


4. Kevin Hoffman left...
Wed 28 Mar 07 11:43 am

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.


5. Jon Hendry left...
Thu 29 Mar 07 8:27 am

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.

At least Objective-C uses somewhat meaningful prefixes. Delphi's pascal had a convention where all classnames should start with 'T' (ie, TFoo, TBar, TView, TForm). But not all constructs with a T were classes. So the T was a fairly useless appendage.


6. Jon Hendry left...
Thu 29 Mar 07 8:32 am

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.

And when properties use a similar pattern... whoo boy, reading logs is a treat.


7. Kevin Hoffman left...
Thu 29 Mar 07 8:33 am

I hear you on the "T" prefix from Delphi... I hated that :)


8. Kevin Hoffman left...
Thu 29 Mar 07 8:34 am

I love the verbosity of invoking methods in Objective-C, its extremely easy to read.


9. ObjectGuy left...
Thu 29 Mar 07 9:24 am

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.


10. ObjectGuy left...
Thu 29 Mar 07 9:32 am

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.


11. Dirk Theisen left...
Thu 29 Mar 07 10:48 am :: http://www.objectpark.org

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).

7 Years of Cocoa later, I can second his point. Like generic types (C++, Java) explicit name spaces would solve a non-problem in Cocoa.


12. Kevin Hoffman left...
Thu 29 Mar 07 11:47 am

That's an excellent point... Namespaces would solve a non-problem in Cocoa. Very well put.


13. Jon Hendry left...
Thu 29 Mar 07 12:00 pm

In my experience the main problem with the prefixes is coming up with one that is aesthetically pleasing.

;^)


14. James Gregurich left...
Thu 29 Mar 07 12:08 pm

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.


15. leeg left...
Sat 31 Mar 07 4:48 pm :: http://thaesofereode.info

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.


16. Blain left...
Tue 03 Apr 07 1:50 am

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.

One advantage that Obj-C has over C++ and Java is that it's a complete superset of C. "int public=5;" will compile in C. It will compile in Obj-C. It errors out in C++ and Java. That's why all the Obj-C keywords are either in brackets (Thus, won't be valid C anyways) or start with @.

There's a hint of a namespace in bundles, whatwith com.yourcompany.yourapp.plist in the preferences folder. But namespaces have these cons: It doesn't solve the method problem mentioned by others, it could be an awkward mess to use, It carries the danger of finding the right keywords, and the biggest one is that it can obfuscate things.

To a great degree, you can work on an Obj-C function and only keep the mindset of the scope of the function. If you have a namespace declaration at the beginning of the file, that's one more state to keep in mind that might have significant effects. Ironically enough, perhaps the biggest reason against namespaces is that it leads to name conflicts not in the compiler, but in the programmer's head. Besides, the naming conventions for classes and variables and the like is kind of cute.

Also, something that really goes counter to namespaces. Cool and scary at the same time. PoseAsClass if you haven't seen this.


17. Alex left...
Mon 26 May 08 2:24 pm

This is very after the fact, but...

I can present a compelling argument for namespaces in Objective-C (or any language that has ambitions to being used on large systems): As systems get larger and more vendors start providing more toolkits, sooner or later you are guaranteed to have name conflicts. The post mentions class name conflicts, but variable and constant name conflicts are just as problematic. As one comment has noted, so far, this is a non-problem for Objective-C, but I will argue that is only true because Objective-C has essentially only one vendor on one platform.

Let me give an example: Years ago, I had to take a GUI app that had been developed using Borland's OWL toolkit and extend it using Borland's new C++ Builder toolkit. Many of the classes in the two toolkits had the same names, but because I could wrap the old toolkit in a C++ namespace, I was able to keep them separated. Unfortunately, there were also many constants that were defined in both toolkits using the C preprocessor, and those were all namespace independent. This led to many very subtle bugs, and a very complicated build process designed to separate the two source trees as much as possible to avoid conflicts.

Objective-C is vulnerable to these kinds of conflicts, and has protections against neither. The only defence is strict programmer discipline, like: munge your own namespaces by prefixing MyToolKit to all of your class, variable and constant identifiers, and never, ever use the preprocessor to define anything but cross-platform conditional compilation blocks.

At the moment, there are few enough Objective-C toolkit vendors so that this sort of discipline is commonplace and expected. If the history of C and C++ is relevant at all, we can expect that this will not remain true if the platform grows at all. Just think: the best practice suggested in the post was for Super Soft to call their classes SSWhatever. But what happens when Solid Systems puts out a competing library? How will you distinguish between SSCanvas and SSCanvas? If namespaces are available, you could wrap each library in its own namespace and keep them separated. Without namespaces...well...what would you do?


Tag Related Posts

So I'm in the LA Times ;)

Wed 27 Aug 08 2:51 P GMT-05
tags:                  

MobileMe vs. Live Mesh Throwdown - Round 1

Wed 16 Jul 08 10:33 A GMT-05

Building Model Classes in C# and Cocoa

Sun 15 Jun 08 3:13 P GMT-05
tags:            

MobileMe vs. Live Mesh - Round 1

Wed 11 Jun 08 12:20 A GMT-05

NYC SharePoint Developer Needed

Mon 12 May 08 12:09 P GMT-05

My Macbook Air is masculine, dammit!

Mon 17 Mar 08 6:59 P GMT-05
tags:          

iPhone Underrated as a Gaming Device?

Fri 14 Mar 08 1:50 P GMT-05
tags:        

My take on the iPhone SDK

Sat 08 Mar 08 1:39 P GMT-05

Jobs says "not likely" to Flash on the iPhone

Thu 06 Mar 08 1:39 A GMT-05
tags:          

My Macbook Air Review

Sun 02 Mar 08 4:20 P GMT-05

iPhone Roadmap March 6th

Fri 29 Feb 08 10:41 P GMT-05
tags:        

Video of the Macbook Air in Action

Wed 20 Feb 08 3:04 P GMT-05

Macbook Airはきれいですよ!

Sun 17 Feb 08 2:38 A GMT-05

Why is O'Reilly Condoning iPhone Hacking?

Mon 11 Feb 08 3:55 P GMT-05

Evaluating my next laptop purchase

Wed 06 Feb 08 8:40 P GMT-05

The iPhone SDK key has been leaked! Oh Noez!!!1

Tue 29 Jan 08 11:36 A GMT-05
tags:        

Why Geeks just don't "get" the Macbook Air

Thu 17 Jan 08 2:30 P GMT-05

Popcorn + TiVo + Macbook Pro + iPhone == Hell Yeah!

Tue 15 Jan 08 3:11 P GMT-05
tags:          

How my ADC membership changed my life

Mon 31 Dec 07 3:57 P GMT-05
tags:      

Leopard Code Sample : Sprinkling in some Bonjour

Tue 27 Nov 07 2:32 P GMT-05
tags:        

Leopard Sample: A Bound NSCollectionView

Mon 29 Oct 07 1:41 A GMT-05

Leopard is out - let the code samples begin!

Fri 26 Oct 07 10:09 A GMT-05
tags:          

My life is complete : iPhone SDK is CONFIRMED.

Wed 17 Oct 07 6:38 P GMT-05
tags:          

Leopard Shipping October 26th!!

Tue 16 Oct 07 4:59 P GMT-05
tags:        

My iPhone Review

Mon 23 Jul 07 1:09 P GMT-05
tags:        

Microsoft Codename Acropolis - Unwrapped

Wed 20 Jun 07 3:22 P GMT-05
tags:              

The dreaded language bleed-over has begun

Tue 19 Jun 07 6:23 P GMT-05
tags:        

Exploring the Delegate Design Pattern

Mon 14 May 07 6:30 P GMT-05

Core Data - Almost too Easy?

Wed 18 Apr 07 2:23 P GMT-05

Will Silverlight be DOA?

Mon 16 Apr 07 8:02 P GMT-05