TweetFollow Us on Twitter

Sep 01 Programming2

Volume Number: 17 (2001)
Issue Number: 09
Column Tag: Programming Philosophy

Mac vs. Unix traditions

by Marcelo Amarante Ferreira Gomes

How to combine the best of both with MacOS X without gathering the worst

A Big Move

With the advent of Mac OS X, Apple has departed from the traditional Mac OS system software to a new model, that of Unix. This has brought a great impact, both upon users and developers. In this article, I'll concentrate on the impact it has on us, developers.

Mac OS X brings a lot of changes. We now have a single filesystem tree, for instance, instead of a tree for each mounted volume; we have lots of users, instead of the few we had with Classic, even though most are fake, like root or bin; we have a much more complex concept of process ownership, with process groups, effective and real user and group IDs and a controlling tty. These - and lots of other - changes also change the way we code, something that even resembles the move from 68k to PPC in 1994, but is a lot closer to the move from System 6 to System 7 in the late 80's and early 90's.

There were no new things to think of when we moved from 68k to PPC, only lots of programming pitfalls. But when System 7 came up, later to be renamed Mac OS 7, we actually had to think different. Those readers old enough to remember will recall that when System 7 came up, we had to think of High-level events, mostly Apple events, for simple tasks like opening a document, and try to use them whenever possible, instead of the low-level events and Finder lists we were used to. We had to revamp our user interface, so that our applications wouldn't look displaced in that new and way cooler-looking GUI. We had also to do away, once and for all, with our 24-bit stuff and use 32-bit-clean pointers and handles, paying attention to the possibility of the use of virtual memory in the system.

For those of you new to Macintosh, but experienced Unix developers, there were a few similar moves that the Unix community has gone through, too. They have been much smoother, since there was no single company dictating directions to developers, like Apple does in the Mac arena. To name a few, there was the move from the single mainframe with lots of terminals to lots of networked personal workstations; from UUCP and serial-based WAN links to TCP/IP and Ethernet-based LANs; from character-based UI to X-based GUI; from single-computer passwd files to distributed NIS and/or shadow user databases. Some of these were actually additional options, rather than changes. For instance, I hardly recommend the use of NIS, even though I do recommend shadow password files. And even today, I still recommend UUCP links and character-based UI in specific cases. But either the new features or the changes in them required some extra care in the way one writes a new piece of software, and I'm not referring only to programming pitfalls here.

I could go on, naming other changes like these, both in the Mac and Unix fields, but the important thing here is to point out that there are some major moves in development scenarios from time to time. And when such a move brings lots of changes at once, it also produces changes in the programming philosophy. That's what's happening with the introduction of Mac OS X. And those of us who fail to perceive this will probably also fail to survive in a market that is always hungry for state-of-the-art technology.

What's A Programming Philosophy, Anyway?

It's not only about programming caveats. The programming philosophy encompasses the way we design, we use, and even think about the uses for a computer system. Most Macintosh users think of their computers almost as a friend, while Unix users tend to think of them only as a tool to get some job done.

Mac OS has traditionally been very user friendly and highly customizable but very consistent through different applications, both in terms of the interface with the user and the functionality. Unix, while highly customizable in functionality - even more than the Mac in most aspects - is usually very harsh with its fixed user interface. It seems to have been designed to work as a server, while the Mac OS is a great workstation.

Veteran developers always take these facts into account, even if unconsciously, and write code that corresponds to their perception of what the user needs. What Apple is doing now with Mac OS X is bringing the two worlds together, releasing an OS that is great for both a server machine and a workstation. This means that Apple wants users to have the same consistent user interface through all pieces of software they interact with, while having rock-solid system stability and compatibility.

Give Users What They Need

To build a successful Mac application, one must deliver what the user needs. Note that this does not mean that you should code just what the user expects. You can and should surprise the user with new and improved ways to do something. But try to invent features that users would guess when presented with a given screen. Developers with no previous Mac programming experience should observe mainly the Mac human interface guidelines. The typical Mac users expect every application to be similar to all others. They are used to reading the manual only as a last resort. Applications should be intuitive to them.

Apple has defined the recommended human interface guidelines required to achieve a successful Mac application long ago. These are constantly being improved, as new creative ways to interface with the users are developed, but the basic rules are always the same. What they say can be summarized as:

  • when you add a feature to your software, do it in a way that resembles something you've seen before in another software;
  • if the feature you're adding is radically new, try to imagine what the user would do to access that feature. Better yet, make a prototype of your application and have long-time Mac users play with it for some time. Pay attention to what they try to do, and listen to their comments very carefully.

There are lots of standard Mac OS interface elements that can be used to give users a clue to what they should do to get something done. If your particular feature does not fit into any interface elements, you should probably talk to a veteran Mac user about it. If you both decide there's no interface element that fits your needs, then try to invent your new element as similar as possible to another existing element. And always provide clear visual clues of the way your new element works.

Be Compatible - And International

In Mac OS, as well as in Unix, there are some conventions about where to put settings files, support code libraries, font files and lots of other things. Unlike in Unix, though, these special places should not be referred to by name. Apple has introduced the FindFolder system call a long while ago to give your software the path to these places. You can find the official Apple documentation on FindFolder at [1].

Under the Cocoa programming environment in Mac OS X, Apple thought you wouldn't need FindFolder, so they provided NSUserDefaults instead to store settings. See [2] for more info.

I see some of you asking: "why should I care about making a system call to find out what I already know?" This is a common pitfall. Settings files should go into the System Folder:Preferences folder, right? Not always. Under the Brazilian version of Mac OS, for instance, they should go into the Pasta do Sistema:Preferências folder. Besides, always making sure to call the proper system functions enables Apple to restructure the filesystem layout without breaking any software and without having to support two folder structures at the same time.

With software that don't call FindFolder, international users end up with some settings files in the correct place while some other software create folders with foreign names and store their preferences there. Try to explain that to a user!

A much worse situation comes up with installer software. Say you need to put a certain file in a special folder, like System Folder:Extensions in Mac OS Classic, or to alter one of the rc.* under the /etc in Mac OS X. If you don't make the appropriate system call, your file may end up in the wrong place, and your software simply won't work. Please note that I, as a novice Mac OS X programmer that we all but a few Apple engineers are, haven't been able to find what the call would be to find the proper /etc directory or the rc.* files. Maybe it doesn't even exist, in which case Apple should provide us with such call.

Don't Mess Things Up

The same "don't mess up with the filesystem" can be said to wannabe Unix developers. Developers with no previous Unix experience must take into account various aspects of the Unix culture, especially those that have security implications. Apple itself has made some significant departures from traditional Unix practice.

For instance, most, if not all, Unix variants have a real directory for /etc. With Mac OS X, Apple has decided to make /etc be a symbolic link to /private/etc. There are other systems that put a symbolic link where a real directory would be, most notably Solaris with its /bin being a link to /usr/bin, but generally there's a good reason for this kind of change.

I don't know what was Apple's reason for putting a symbolic link in the place of /etc. The only thing I can think of is to make it easier to change most of the system's configuration by just making the link point elsewhere.

But this kind of change is dangerous, given standard Unix programming practice and system call side effects. It must be done at a very carefully chosen time during the boot process. It could also be done while the system is up, but one should be aware of the implications of this huge change in his or her particular system.

One problem that could appear stems from the fact that when a process opens a file, it gets a file descriptor that refers to whatever has been named by the time the open system call has been made. After that call, it doesn't matter if that file is moved, renamed or even deleted: the file descriptor sticks with the originally opened file. In case of deletion, the disk space is freed only when all of the processes that have opened it have also closed it. With all this, if one ever changes the symbolic link in /etc, there is the risk that one process, for instance, a server started at boot time, has a configuration file open that doesn't correspond to the configuration seen by all other processes started after the link has been changed.

The extent of damage caused by such situation can range from nothing at all to a completely disastrous crash. Or, worse yet, it can pose a security threat, for instance, when a client process thinks that the server will do some sort of security check, while the server thinks that check to be the client's responsibility.

Don't Reinvent the Wheel

Another departure Apple has taken from standard Unix with its Mac OS X was the use of the little known pax archiving utility for its installer process. I would personally have gone with tar, but I wouldn't mind Apple to have chosen cpio instead, since these two are the formats that most (99.9 percent?) die-hard Unix fans prefer to distribute software in.

With the use of pax, Apple has introduced some serious security flaws in their installer process. I have read a great article about this issue, but regrettably, I can't seem to find the URL in my bookmarks. Anyway, the main concern in that article is that pax does not deal very nicely with permissions and ownership data about already existing directories and files. Even worse, if you change a directory for a symbolic link, pax may get confused and delete the link, replacing it with a real directory. If the link had been placed there in order to save disk space and have the tree below it stored in another disk partition or other physical disk, you will run into big trouble.

Pax seems to have been designed to create all of the directories and files that it is installing, and it's not very clever to leave things like they are when it finds something already in place. That way, whenever you install new software, you risk introducing security holes that have long been corrected.

Until Apple fixes this, either by using tar or cpio instead of pax, or by making pax a little smarter, we will have to double-check our systems every time we install something. The easiest way to do so seems to be by looking at the installer package, taking note of all of the files it adds, along with the directories they reside in, and checking for their correct ownership, permissions and file type (regular file, directory, symbolic link, device file, etc.) Obviously, this work would be much easier if you take some time to store all of this information before the installation takes place and make any needed corrections afterwards.

Summing it All Up

Lots of other aspects about good development habits could have been mentioned here, both under Unix and Mac OS, but a magazine article is just too short to even give a good coverage, let alone being complete. The examples given here serve only to give you an idea of what kind of downturns you might run into by not being aware of standard practice.

But they also serve to give you an idea of what a programming philosophy is. It's about the way developers think, and the "right" way of doing things. Everyone will have a different idea of what is the "right" way of doing something, but some basic ideas are generally agreed upon among developers from a given OS. These basic ideas are what is being referred to here as a programming philosophy.

How to Get There

Ok, we have to develop a new programming philosophy, that of Mac OS X, which should be a little different from Classic Mac OS and from Unix, but should not steer too far from either. But how do we get there? That depends on whether you are coming from Mac OS, from some sort of Unix or you are completely new to both. Either way, you've got to take a look at some web page from [3], especially [4].

Mac OS X for Classic Mac OS Developers

Of course Apple wouldn't succeed if they wanted us to rewrite all of our dependable code, that has cost us years of testing and debugging effort. But Apple can't stand still. So, they have provided us with three environments: Classic, Carbon and Cocoa. Ok, you've heard that before, so I'll skip to the important stuff.

Classic takes care of users. They won't have to buy new versions of all of their software title before moving on to X, but it's discontinued now, and it's not worth writing anything new for that environment. Cocoa, on the other hand, is the all-new innovative programming environment, in which all the good stuff is. Unfortunately for us who have invested on C, C++, Pascal or other languages, only Objective-C and Java APIs are available to work with it.

Then, there's Carbon. The environment for those coming from Classic Mac OS is definitely this one. Carbon is something in between Classic and Cocoa. It doesn't offer all the cool stuff that Cocoa offers, but it does offer most of it, including most of the coolest features we've been hearing about Mac OS X. It allows you to move your applications from the old Classic environment with little effort. Better yet, if you write for Carbon, your code may be able to run on Mac OS 9 and, depending on the set of system calls you choose, even on 8.x, by just having CarbonLib installed there. You can learn more about it in [5].

You can still think of files most the same way you did before, call most of your favorite system calls and think of the system mostly as if it really were the familiar Mac OS you've always known - for a while. It won't take long before you start cursing those most words in the previous sentence. You'll want to move on to Cocoa. Carbon is just a bridge to make that transition smoother.

And please try to stick to the rules. Learn the Aqua human interface guidelines (see [6]) and Quartz new API calls that are needed to implement some of Aqua look and feel (see [7] and [8].) Don't just use the same old ways of interfacing with the user. Some of the old methods, like custom MDEFs or WDEFs won't work, anyway.

Mac OS X for Unix Developers

By now, you already know that Cocoa is for you. Or isn't it? That depends on your previous knowledge of either Java or Objective-C or your willingness to learn a new programming language. If you already know any of these two languages, then look no further. You've got to go the Cocoa way, so read [9].

But if you are a seasoned C, C++, Pascal, Fortran, Basic, or something else programmer, you might want to give Carbon a try, at least for a while. Be warned, though, that you'll have a much harder time than your fellow Classic Mac OS programmers trying to go this way. While they already know most of the philosophy involved in writing good Mac OS software, you'll have to learn that from the beginning.

The Aqua API will be something new, and don't even think about writing an X-windows application. Most Mac OS X systems won't even have X-windows installed. But don't let that discourage you. The concepts involved are very similar. You still have a main() function calling an event loop and acting on events, just like on X-windows. Only the API will be new. Well, not only the API - you'll have to play by the Aqua rules. Read [6]; reread [6], then read it once again. Tired of reading? Read it backwards once more, just in case. It's never too much to stress how important the GUI rules are to keep Mac OS consistent.

"Damn, I could forget about all this GUI stuff and write a character-only app," I hear you saying. Ok, go ahead. But don't forget what your intended audience is. The average Mac OS user doesn't even know what a character-based UI is, let alone how to get to a command prompt. But if your audience is just a bunch of colleagues of yours and you do write a character-based application, please play by the traditional Unix rules, and try not to mess up with the System Folder or some other Mac OSisms brought over from Classic to Mac OS X.

Mac OS X for Newbies

If you've read the previous sections, you stand a good chance of already knowing which way to go. You don't know any programming language at all? Learn Java and/or Objective-C and go the Cocoa way. You already know some C and only want to know what this fruit-branded OS is about? Ok, try Carbon.

No matter what environment you choose to start working with, please, please, please! Follow the rules. All of them.

The Bottom Line

So, no matter what your background in some other OS is, if you don't have a good knowledge on both Unix and Mac OS standard practices, please reserve some time to learn about the culture on these systems. And don't try to save time on this! It will take lots of reading until you can say you're ready to write a successful application for Mac OS X. If at all possible, talking to veteran developers and users should take much of your time, too. But you will spend much more time, money and effort by going ahead coding, writing manuals, license agreements and other documentation, investing on marketing and distribution, only to find out that the users didn't like your software.

Worse than that, when many developers start coding without regard to standard practice, eventually some of them will come up with exceptionally good software that users will like and use although it doesn't follow standard practice. It may even happen with your title. But don't be happy with that.

When a significant number of software titles disregarding minimal guidelines start to gain acceptance, there will be nothing to call standard practice anymore. And Mac OS won't have the consistency it has today. Users will have a hard time figuring out how to use software; various titles will start being incompatible with one another. Then everything Apple and the Unix community across the globe have fought for and accomplished during many years will have been thrown away.

Apple fails to comply with traditional Unix practice in a few spots with dangerous consequences, but those trouble spots are simple to fix. They seem to be doing an overall good job and moving in the right direction. Let's hope that all - or at least most - of us will also get this right, and let's make Mac OS X a success by combining the best of Classic Mac OS with the best that Unix has to offer. And let's not pour in the worst by accident.

Acknowledgements

I'd like to thank Jacques do Prado Brandão, my friendly neighbor who, even not knowing a thing about computers or operating systems, was always there to help me revise some grammatical glitches that stupid word processors can't, as well as keeping a straight line of thought throughout the whole article.

Someone else to mention is Sergio de Souza Prallon, a fellow Brazilian consultant who has read - and understood - this article in its entirety. Being a Unix-only guy, he came up with the idea of writing the entire How to Get There section. Then he lied to me and told me it was good. :-)

And I could never forget about Neil Ticktin, MacTech's publisher. He has told me that the ideas I have presented here could make up a good article, thus giving the spark that ignited this whole thing. Has it blown anything up?

References


Marcelo Amarante Ferreira Gomes is an independent Macintosh, Unix and Internet consultant. He works most of the time with his Brazilian partners, and plays most of the time with his kids and/or his Lombard Powerbook that has the single most important OS in the world, besides most other junk in this industry. You can reach him at suporte@mac.com... if you're lucky. :-)

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.