6.1 KiB
Unix Philosophy
Unix philosophy is one of the most important and essential approaches to programming which advocates great minimalism and is best known by the saying that a program should only do one thing and do it well. Unix philosophy is a collective wisdom, a set of recommendations evolved during the development of one of the earliest operating systems called Unix, hence the name. Unix philosophy advocates simplicity, clarity, modularity, reusability and composition of larger programs out of smaller programs rather than designing huge monolithic programs as a whole. Unix philosophy, at least partially, lives on in many project and Unix-like operating systems such as Linux, has been wholly adopted by groups such as suckless and LRS (us), and is even being expanded in such projects as plan9.
In 1978 Douglas McIlroy has written a short overview of the Unix system (UNIX Time-Sharing System) in which he gives the main points of the system's style; this can be seen as a summary of the Unix philosophy (the following is paraphrased):
- Each program should do one thing and do it well. Overcomplicating existing programs isn't good; for new functionality create a new program.
- Output of a program should be easy to interpret by another program. In Unix programs are chained by so called pipes in which one program sends its output as an input to another, so a programmer should bear this in mind. Interactive programs should be avoided if possible.
- Program so that you can test early, don't be afraid to throw away code and rewrite it from scratch.
- Write and use tools, even if they're short-lived, they're better than manual work. Unix-like systems are known for their high scriptability.
This has later been condensed into: do one thing well, write programs to work together, make programs communicate via text streams, a universal interface.
Example: maybe the most common practical example that can be given is piping small command line utility programs; in Unix there exist a number of small programs that do only one thing but do it well, for example the cat
program that only displays the content of a file, the grep
program that searches for patterns in text etc. In a command line we may use so called pipes to chain some of these simple programs into more complex processing pipelines. Let's say we want to for example automatically list all first and second level headings on given webpage and write them out alphabetically sorted. We can do it with a command such as this one:
curl "https://www.tastyfish.cz/lrs/main.html" | grep "<h[12]>.*</h[12]>" | sed "s/[^>]*> *\([^<]*\) *<.*/\1/g" | sort
Which may output for example:
Are You A Noob?
Some Interesting Topics
Welcome To The Less Retarded Wiki
What Is Less Retarded Software
In the command the pipes (|
) chain multiple programs together so that the output of one becomes the input of the next. The first command, curl
, downloads the HTML content of the webpage and passes it to the second command, grep
, which filters the text and only prints lines with headings, this is passed to sed
that removes the HTML code and the result is passed to sort
that sorts the lines alphabetically -- as this is the last command, the result is then printed. This is fast, powerful and very flexible way of processing data for anyone who knows the Unix tools. Notice the relative simplicity of each command and how each works with text -- the universal communication interface.
Compare this to the opposite Window philosophy in which combining programs into collaborating units is not intended or even purposefully prevented, and therefore very difficult, slow and impractical to do -- such programs are designed for manually performing some predefined actions, e.g. painting pictures with a mouse, but aren't made to collaborate or be automatized, they can rarely be used in unintended, inventive ways needed for hacking.
Watch out! Do not misunderstand Unix philosophy. There are many extremely dangerous cases of misunderstanding Unix philosophy by modern wannabe programmers. One example is the hilarious myth about "React following Unix philosophy" (LMAO this), supposedly the "devs" think that having billion of dependencies or focusing on doing one huge thing (GUI) somehow implies Unix philosophy -- nothing based on JavaScript can ever follow Unix philosophy! Unix philosophy can NOT be built on top of non-unix philosophy technology, and focusing on a very broad goal does not mean doing one thing.
{ One possible practical interpretation of Unix philosophy I came up with is this: there's an upper but also lower limit on complexity. "Do one thing" means the program shouldn't be too complex, we can simplify this to e.g. "Your program shouldn't surpass 10 KLOC". "Do it well" means the programs shouldn't bee too trivial because then it is hardly doing it well, we could e.g. say "Your program shouldn't be shorter than 10 LOC". E.g. we shouldn't literally make a separate program for printing each ASCII symbol, such programs would be too simple and not doing a thing well. We rather make a cat program, that's neither too complex nor too trivial, which can really print any ASCII symbol. By this point of view Unix philosophy is really about balance of triviality and huge complexity, but hints that the right balance tends to be much closer to the triviality than we humans are tempted to intuitively choose. Without guidance we tend to make programs too complex and so the philosophy exists to remind us to force ourselves to rather minimize our programs to strike the correct balance. ~drummyfish }