views:

841

answers:

8

What are some specific[1] real-world code[2]/design examples, personal anecdotes, mistakes learned (with more emphasis on personal anecdotes, mistakes learned instead of pointing out third-party software) that best illustrate the programming philosophy of Unix? For a start, here's Raymond's summarization of the philosophy from The Art of Unix Programming:

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.

[1]: just saying "Linux" (or Unix tools, or my pet project Foo) is not enough; what is it about Linux that makes it an example of the Unix programming philosophy? In a similar manner, just saying "Linux is modular" too is disingenuous; again - what makes linux follow the rule of modularity as listed here? (for eg: lkm, etc.) The devil is in the details.

[2]: This question is not about merely "code" examples as S.Lott seems to believe. The question clearly also states design, personal anecdotes and mistakes learned, with more emphasis on the later two (for which no answer has been given yet).

+1  A: 

Since the Linux OS is a significant real world example of this, what more do you need? Other pieces of software that fix the Linux pattern? Most of the widely-used, very active open source projects that run under Linux fit this pattern. That's a lot of real-world examples.

S.Lott
Just edited the question: I am asking for specific design mistakes/alterations/ideas that one came across one's own or others' projects. For eg., a Linux kernel developer may recall an instance where he unconsciously over-designed a kernel module upon realizing the complexity of which, he sets out to simplify the design. It is this tangible/real-world example that this question is about. The level of detail may be at the class/module/design level.
Sridhar Ratnakumar
Also note that the title says "programming examples," not - for instance - "software examples" or "OS examples".
Sridhar Ratnakumar
@Sridhar Ratnakumar: How is an OS not **programming?**
S.Lott
@S.Lott: what I mean is that this question is about specific programming examples (at the lower/specific abstraction level of classes/modules/design) instead of mentioning the name of a complete software product (linux, autotools, etc..) without telling *why* that product adheres to the unix philosophy; answering the 'why' in terms of *specific* code/design examples. see the update question.
Sridhar Ratnakumar
For the maximum effect, if I told you this, what'd be your response? *"Since the Windows OS is a significant real world example of this, what more do you need? Other pieces of software that fix the Windows pattern? Most of the widely-used, very active open source projects that run under Windows fit this pattern. That's a lot of real-world examples."* I hope I made myself clear.
Sridhar Ratnakumar
@Sridhar Ratnakumar: "what'd be your response?" I'd agree on Windows. Not the MS Office suite of tools, since they don't have **Composition**. But much of Windows **does** adhere to elements of this philosophy. What's your point? You have three examples. What more do you want?
S.Lott
@S.Lott - I don't know whether Windows adheres to most of the unix philosophy or not; I used it as a possible example of that which does *not* adhere to the unix philosophy for most port. With that in mind, if you re-read the above comment - you'd see what I meant. As to what more do I want - what I want is the details.
Sridhar Ratnakumar
@Sridhar Ratnakumar: "details"? What details? Other than the code itself, what details could you want? Can you identify what you want to know that's not the code itself? "Unix Philosophy" **is** embodied by Unix and Linux. So, the logical detail is to then read the code. Clearly, you're rejecting the Unix code which reflects the Unix Philosophy. So, I ask -- again -- **What do you want**?
S.Lott
@S.Lott: read the footnote [1] in the question, and then re-read my first comment to your answer ... in conjunction with my comment above that asks `what makes the Linux codebase modular [in contrast to a hypothetical one that does not]`. I cannot be any more explicit than this.
Sridhar Ratnakumar
@Sridhar Ratnakumar: The codebase **is** modular. What makes it modular is the fact that it **is** modular. Since it **is** modular, I'm not sure what more information can be provided. It **is** modular. Do you want someone to quote a piece of code with the words "See, it's modular"? Is that what you want?
S.Lott
@S.Lott: Just what exactly do *you* mean by 'modular'? I suspect you are undermining the 'Rule of modularity' as explained in http://www.faqs.org/docs/artu/modularitychapter.html thereby limiting its definition to lowest abstraction level comprising of functions/files. When I ask 'what makes the Linux codebase modular' I am specifically referring to modularity in higher abstraction level (eg: gimp plugins) ... otherwise just any program/software written in a procedural language like C can be said to be 'modular' by [the limited] definition. I did not ask this question to merely get truisms.
Sridhar Ratnakumar
@Sridhar Ratnakumar: What? I'm not asking the question. You are. If the answer is "GIMP Plugins", then why post the question? The Unix examples of modular are the Unix code base. Not a definition you created. You're reading about Unix philosophy. That philosophy is **derived** from studying the unix code base. Read @Roger's answer.
S.Lott
@S.Lott: First of all, the answer is not 'GIMP plugins' - I expect it to be much more than that: have you ever noticed the "more emphasis on" part in the question? Secondly, I never created any definition (nor is this question merely about 'unix examples'); it is you who first narrowed down the focus on modularity (out of other rules listed in the question), and then went on to fixate on lowest abstraction level of modularity (otherwise, why ask "Can you identify what you want to know that's not the code itself?").
Sridhar Ratnakumar
@S.Lott: ... Further have you ever noticed how all this discussion would have been avoided in first place were you to accept this narrow definition of yours? (narrow - in relation to how the word 'modularity' is used in the question). For starters, I posted this question with the intent to learn what others have learned from mistakes/realizations of their own projects primarily (w.r.t to the unix philosophy, that is).
Sridhar Ratnakumar
@Sridhar: 1. The answer is more than GIMP. It's all of Linux. It all adheres to the Unix principles. Any random block of code from Unix will show the principles because (as @Roger said) Unix came first, the principles summarize the code. 2. Modularity is one example. I hate to repeat the 17 rules each time I try to figure out what you want. The Linux Code Emobodies the Unix Design Principles. By Definition (since the principles come from the code.) **What More Do You Want?**
S.Lott
@S.Lott: Answered several times (re-read the conversation). I am not going to flog the dead horse anymore.
Sridhar Ratnakumar
@Sridhar Ratnakumar: The horse is not dead. You are merely refusing to say what you need other than the Unix code on which the unix principles were based. Code came first. Principles came second. The code is MORE fundamental than the principles. What more do you need? The code is SPECIFIC. If you want something more specific than the code which lead to the book, please update the question to provide another word that's not the word "SPECIFIC". The code is the most specific example of the book, since the code created the book.
S.Lott
@S.Lott: In a word - 'design' (not very different from what one scribbles on one's notebook before writing a piece of non-trivial software). I've also updated the question to further elaborate this point.
Sridhar Ratnakumar
A: 

Autotools. It's true. I'm sorry.

Samuel Danielson
You really have to be more specific than that. Why do you think autotools follows unix philosophy (above)? Which part of the autoconf system/design/code proves that?
Sridhar Ratnakumar
+2  A: 

Dare I suggest Windows PowerShell? I believe it embodies the philosophy better than Unix itself. For those unfamiliar, here's what Wikipedia has to say about it: http://en.wikipedia.org/wiki/Windows_PowerShell

To summarize, it's designed to be an ecosystem of small objects that can all communicate with each other to perform complex tasks.

Gabe
+3  A: 

Try 'The UNIX Philosophy' by Mike Gancarz, rather than listening to ESR. In his book he mentions the 'MH' mail client:

http://rand-mh.sourceforge.net/

mh is a small series of programs you can use in conjunction with other unix utilities to read mail.

Check out the design of MH http://www.rand.org/pubs/notes/N3017/ And the manual http://www.rand.org/pubs/reports/R2367/

I'd also recommend Plan9 as a wonderful example of something more UNIX than UNIX. Everything is a file within plan9 and it.

1729
pra
Thank you for the pointer to Mike's book ... was flipping through the Kindle sample and decided to buy and read it.
Sridhar Ratnakumar
+2  A: 

To my mind, the highest ideal of UNIX programming is to have a bunch of very small programs who "do one thing well" and are composable, usually through PIPEs and usually processing data as text.

Other styles of programming also have this philosophy, albeit in different forms. OOP has the Single Responsibility Principle, functional programming has combinators, higher-order functions, monads, and so on. The Linux OS exhibits the UNIX philosophy quite well, particularly in the way it loads and unloads independent modules, but it has a reasonably large kernel at its core. GNU Hurd took the idea a step further and got rid of Linux's macrokernel core and attempted to produce an entire OS based on small, independent, cooperating parts, although Hurd itself never really took flight.

Concrete examples

In terms of examples of the UNIX philosophy, there are a few places to look. Good shell scripting practice should exhibit UNIX-foo, as should well written Emacs modes and some kernels -- try MINIX if you don't want something small and readable: http://www.minix3.org/source.html

For a medium-sized example of shell scripting, the BASH script in /etc/init.d/skeleton is a skeleton for a UNIX daemon and, to my mind, written with some clarity. You can read more about this here (with source): http://www-theorie.physik.unizh.ch/~dpotter/howto/

Some smaller examples of shell scripts, which (IMO) exhibit the UNIX philosophy, follow:

Get an accurate word count of a LaTeX document split over several files

detex mydocs/*.tex | wc -w

Check for large files in an SVN repo

svn status | awk '{print $2}' | xargs du | sort -n | tail

List files by modification date

find -type f -print0 | xargs -r0 stat -c %y\ %n | sort

With all of these examples we're just combining simple building blocks to produce a single result. You can find a whole bunch of scripts like this at http://www.commandlinefu.com

snim2
A: 

All Unix tools embody these ideas. Unix came first. The book came later. That should tell you something.

Roger
A: 

Git is an epitome of the software built with Unix philosophy.

On installing git, you install literally 100s of commands on to your system, of which most are called git-plumbing which git uses internally and 36 are the high level commands, called git-porcelain that abstract the plumbing usage and hides the implementation detail to (well, ostensibly) create good interface.

Lakshman Prasad
+1  A: 

In the real world applications, django definitely has a strong Unix philosophy.

You want to add a "email users and confirm their email, on registration", add an app: (django-registration)[http://bitbucket.org/ubernostrum/django-registration] and point it to an url pattern. You want to add "Add ability to login via Facebook, twitter", add another application: Django-Socialauth and point it onto another url path.

Even the applications themselves, "Do one thing, and do it well" Very unix like.

Lakshman Prasad