Unix philosophy
|
The Unix philosophy is a set of cultural norms and philosophical approaches to developing software based on the experience of leading developers of the Unix operating system. Many individuals have examined these norms and tried to summarize them in some way.
Contents |
McIlroy: A Quarter Century of Unix
Doug McIlroy, the inventor of Unix pipes and one of the founders of the Unix tradition, summarized the philosophy in Peter H. Salus' A Quarter Century of Unix as follows:
- "This is the Unix philosophy:
- Write programs that do one thing and do it well.
- Write programs to work together.
- Write programs to handle text streams, because that is a universal interface."
This is usually severely abridged to "Do one thing, do it well."
Of the three tenets, only the third is specific to Unix, though Unix developers often emphasized all three tenets more than other developers. It has been argued that the sequential text-stream processing paradigm has become somewhat less universal over time, as GUI applications and distributed messaging have become more prominent. However, GUI applications are now often controlled by text streams (e.g., using HTML, XML, and specialized languages such as Mozilla's XUL). Distributed messaging is also often implemented by text streams (e.g., SOAP and XML-RPC). Thus, the idea of using text stream handling to create solutions is still widespread even in those domains.
Pike: Notes on Programming in C
Rob Pike, a leading expert on the C programming language, offers the following "rules" in Notes on Programming in C as programming maxims, though they can be easily viewed as points of a Unix philosophy:
- Rule 1. You can't tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don't try to second guess and put in a speed hack until you've proven that's where the bottleneck is.
- Rule 2. Measure. Don't tune for speed until you've measured, and even then don't unless one part of the code overwhelms the rest.
- Rule 3. Fancy algorithms are slow when <math>n<math> is small, and <math>n<math> is usually small. Fancy algorithms have big constants. Until you know that <math>n<math> is frequently going to be big, don't get fancy. (Even if <math>n<math> does get big, use Rule 2 first.)
- Rule 4. Fancy algorithms are buggier than simple ones, and they're much harder to implement. Use simple algorithms as well as simple data structures.
- Rule 5. Data dominates. If you've chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.
- Rule 6. There is no Rule 6.
Pike's rules 1 and 2 restate Tony Hoare's famous maxim "Premature optimization is the root of all evil." Ken Thompson rephrased Pike's rules 3 and 4 as "When in doubt, use brute force.". Rules 3 and 4 are instances of the design philosophy KISS. Rule 5 was previously stated by Fred Brooks in The Mythical Man-Month. Rule 5 is often shortened to "write stupid code that uses smart objects". Rule 6 was previously stated by Bruce in Monty Python's Flying Circus.
Gabriel: Worse is Better
Richard P. Gabriel suggests that a key advantage of Unix was that it embodied a design philosophy he termed Worse is better. In the "Worse is better" design style, simplicity of both the interface and the implementation is more important than any other attribute of the system — including correctness, consistency and completeness. Gabriel argues that this design style has key evolutionary advantages, though he questions the quality of some results.
For example, in the early days UNIX was a monolithic kernel (which means that user processes carried out kernel system calls all on the user stack.) If a signal was delivered to a process while it was blocked on a long-term I/O in the kernel, then what should be done? Should the signal be delayed, possibly for a long time (maybe indefinitely) while the I/O completed? The system call could not be executed when the process was in kernel mode, with sensitive kernel data on the stack. In these cases Kernighan and Ritchie favored simplicity over perfection. The UNIX system would occasionally return early from system call with an error stating that it had done nothing - the "Interrupted System Call" - an error number 4 (EITR) in today's systems. Of course the call had aborted in order to call the signal handler. This could only happen for a handful of long-running system calls, i.e. read(), write(), open(), select(), etc. On the plus side, this made the I/O system many times simpler to design and understand. The vast majority of user programs were never affected because they didn't handle signals and would die right away if one was raised. For a very few number of programs - things like shells or text editors - that respond to job control keypresses - these programs could put small wrappers on their system calls and retry the call right away if this EITR error was raised. Problem solved, in a simple way.
The Multics philosophy was "the system must be perfectly general at all costs!" and the kernel developers suffered heavily for this perfectionism. Every long-running Multics call at various points would have to support complex save and restart procedures, easily doubling or tripling the complexity of a major part of the Multics kernel. Because of this philosophy, the Multics kernel was several hundred thousand lines of code, and it was estimated by its developers that a "security subset" of the kernel could be written in "only" 50,000 lines of code. The original UNIX kernel was only about two thousand lines of code, and accomplished the same security goals as Multics, by using a "worse is better than perfectly general" philosophy.
The X Windows System is not an example of the UNIX Philosophy. If one has ever struggled to program for the X Windows System, one finds that it is intended to be all things to all people, and the level of complexity and mental investment required to master the X Windows System is very high.
The EMACS Editor is not an example of the UNIX Philosophy. Few people ever attain any competancy in the emacs editor programming language, and when the editor has loaded for the first time, more than 500 commands are available, commands which most users will never master. This editor attempts to be all editors to all people.
It is surprising that both the X Windows Systm(led by one of the main Multics contributors) and the EMACS editor, were designed by former MIT graduate students after the Multics system had failed, that makes some Unix hackers wonder if MIT systems researchers ever learn from their mistakes.
Raymond: The Art of Unix Programming
Eric S. Raymond, in his book The Art of Unix Programming, summarizes the Unix philosophy as the widely-used engineering philosophy, "Keep it Simple, Stupid" (KISS Principle). He then describes how he believes this overall philosophy is applied as a cultural Unix norm, though it is not hard to find severe violations of most of the following in actual Unix practice:
- Rule of Modularity: Write simple parts connected by clean interfaces.
- Rule of Clarity: Clarity is better than cleverness.
- Rule of Composition: Design programs to be connected to other programs.
- Rule of Separation: Separate policy from mechanism; separate interfaces from engines.
- Rule of Simplicity: Design for simplicity; add complexity only where you must.
- Rule of Parsimony: Write a big program only when it is clear by demonstration that nothing else will do.
- Rule of Transparency: Design for visibility to make inspection and debugging easier.
- Rule of Robustness: Robustness is the child of transparency and simplicity.
- Rule of Representation: Fold knowledge into data so program logic can be stupid and robust.
- Rule of Least Surprise: In interface design, always do the least surprising thing.
- Rule of Silence: When a program has nothing surprising to say, it should say nothing.
- Rule of Repair: When you must fail, fail noisily and as soon as possible.
- Rule of Economy: Programmer time is expensive; conserve it in preference to machine time.
- Rule of Generation: Avoid hand-hacking; write programs to write programs when you can.
- Rule of Optimization: Prototype before polishing. Get it working before you optimize it.
- Rule of Diversity: Distrust all claims for "one true way".
- Rule of Extensibility: Design for the future, because it will be here sooner than you think.
Many of these norms are accepted outside of the Unix community — if not when Unix first used them, then later on. Also, many were not unique or original to the Unix community. Nevertheless, masters of Unix programming tend to accept a combination of these ideas as the foundation of the Unix style.
References
- Notes on Programming in C (http://www.lysator.liu.se/c/pikestyle.html), Rob Pike, September 21, 1989
- The Rise of Worse is Better (http://xahlee.org/UnixResource_dir/_fastfood_dir/worse-is-better.html) — from Lisp: Good News, Bad News, How to Win Big (http://www.ai.mit.edu/docs/articles/good-news/good-news.html), Richard Gabriel, 1991
- A Quarter Century of Unix, Peter H. Salus, Addison-Wesley, May 31, 1994 (ISBN 0201547775)
- Philosophy (http://www.faqs.org/docs/artu/philosophychapter.html) — from The Art of Unix Programming (http://www.catb.org/~esr/writings/taoup), Eric S. Raymond, Addison-Wesley, September 17, 2003 (ISBN 0131429019)
- The Unix Programming Environment (1984 -Brian Kernighan and Rob Pike)
Quotes
"UNIX was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things." -- Doug Gwyn
"Unix never says `please.'" -- Rob Pike
"It is Unix. It is possible to overcome any number of these bogus features." -- pjw