Create classes to represent your model objects
« previous entry | next entry »
Dec. 18th, 2006 | 11:39 pm
Collections are quite widely used in a lot of the code I see posted to the Cocoa-dev mailing list and elsewhere. Unfortunately, many times they're not used to represent collections of objects but rather to represent objects themselves. In one recent pathological case, I even saw some code that used an NSMutableArray as a model object instead of the typical NSMutableDictionary, complete with hard-coded indexes to represent the different properties of the object!
Why's this a problem? This goes back to the core tenet of object-oriented programming: In object-oriented programming, you write software by sending messages to objects, and these objects are state plus behavior. When you use a dictionary or an array as a model object behavior goes out the window — the only behavior you'll get out of that model object is that which makes it a dictionary or array, not anything specific to your application.
Thus even if you're only just going to implement accessors to start with, you're generally better off creating classes to stand behind your model objects because you'll have an easier time moving appropriate behavior into them. And if your application targets Leopard this is extra-easy thanks to the new support in Objective-C 2.0 for properties.
Why's this a problem? This goes back to the core tenet of object-oriented programming: In object-oriented programming, you write software by sending messages to objects, and these objects are state plus behavior. When you use a dictionary or an array as a model object behavior goes out the window — the only behavior you'll get out of that model object is that which makes it a dictionary or array, not anything specific to your application.
Thus even if you're only just going to implement accessors to start with, you're generally better off creating classes to stand behind your model objects because you'll have an easier time moving appropriate behavior into them. And if your application targets Leopard this is extra-easy thanks to the new support in Objective-C 2.0 for properties.
(no subject)
from:
fraserspeirs
date: Dec. 19th, 2006 11:45 am (UTC)
Link
Agreed. The other major benefit I've found is that it makes the code that operates on these models much more readable and maintainable. A call to named (and, importantly, well-named) accessors is much more readable than code accessing collections.
Also, for developers still targeting Tiger and earlier, it's worth mentioning Accesorizer.
Reply | Thread
Great post + a request
from: anonymous
date: Dec. 19th, 2006 02:53 pm (UTC)
Link
Unrelated -- I request that you switch to something other than LiveJournal, and I'll do my best to make sure everyone finds you. The commenting system and a number of other things feel more natural on Blogger and other services. For example, I can't sign the post unless I'm a LiveJournal/OpenID user.
Scott Stevenson
Reply | Thread
Re: Great post + a request
from:
chanson
date: Dec. 24th, 2006 04:39 pm (UTC)
Link
I actually really like LiveJournal. (LiveJournal: It's not just for emo teens! It's also for, uh, emo thirtysomethings...) I know that the lack of support for anything but LiveJournal/OpenID for signing posts is a hassle — hopefully they'll have a third "name and URL" option someday soon — but I actually find most other things about LiveJournal very natural. In particular, I really like the tagging system, the access-control features, and the fact that I've had only the tiniest bit of comment spam over the nearly 5 years that I've been using LJ.
If I were to have separate "personal" and "professional" blogs I'd probably keep this for my personal and set up another site using another service for my professional blog. On the other hand, I'd definitely want to use an engine that was at least comparable to LJ in its feature set (except perhaps on the access-control front), which can actually be a little tough to find.
Reply | Parent | Thread
(no subject)
from: anonymous
date: Dec. 19th, 2006 05:18 pm (UTC)
Link
Reply | Thread
Great thought, and I linked back a response...
from: anonymous
date: Dec. 24th, 2006 09:19 am (UTC)
Link
Reply | Thread
Collections widely used within Cocoa too
from: anonymous
date: Dec. 29th, 2006 12:08 am (UTC)
Link
At first that bugged me, but I've come to appreciate seeing objects and data structures as distinct things. It also helps mitigate the problem of classes communicating without exposing too much of their own inner workings (http://www.cocoadev.com/index.pl?GettersAndSettersAreEvil). Someone in the linked discussion pointed out an interesting quote along these lines:
So while overuse of collections can be a bad thing, the other extreme can also get out of hand. I'm a big fan of the middle path.
Scott
Reply | Thread
Now not a fan of LJ either
from: anonymous
date: Dec. 29th, 2006 12:11 am (UTC)
Link
Scott
Reply | Parent | Thread
Re: Now not a fan of LJ either
from:
chanson
date: Dec. 29th, 2006 12:23 am (UTC)
Link
Reply | Parent | Thread