Disclaimer: This article was originally published in the 10th issue of programmer magazine, which was slightly deleted, non-written permission from the magazine and author, etc. Original author: Greg Colvin http://www.artima.com/cppsource/spiritofc.html
Translation: http://blog.9cbs.net/sese
Do we think that C, C and Java "share some of the same spirit"? At least from the top level, they look very similar. For example, the following code segment, if it places it in the other three languages, it will calculate the number of two numbers in accordance with the Euro Delivery algorithm:
INT G CD (INT M, INT N) {
While (m> 0) {
IF (n> m) {
INT T = m; m = n; n = T;
}
m - = n;
}
Return n;
}
However, when talking about "sharing some of the same spirit", we refer to the more essential common commonality that exceeds the grammar level. In order to find these spirits, let's take a look at the design principle of ANSI C standard:
The Standards Committee is based on the traditional spirit of C. The spirit of C includes many aspects, and its own origin is a sense of identity of the deep principle of language in C language community. Certain aspects of the spirit of C, summarized in the form of a phrase:
1 trust programmer
2 Do not block programmers to do things that should do
3 Keep language small and simple
4 Any operation is only one way to complete
5 Even if the transplantability cannot guarantee efficiency
The last point needs to explain: Compiling the potential of generating efficient code is one of the most important strengths in C language. To ensure that simple operations do not cause the code to be compiled, many operations are defined as hardware decisions by the target machine rather than some universal abstract rules.
In many ways, in fact, B-language is the best embodiment of these spirits. From then, the evolutionary process started can be seen as a constant compromise and compromise on these five principles. I will talk about this later.
"After 20 years of education, you are asked to work in Japan - Boy Daylon
"TWENTY YEARS of Schooling, The Put you on the day shift" --Boy Daylon
Let me talk about my own history of myself and C. As early as 1983, after I was a doctoral degree, I joined my mentor Peter OsSorio, I have just created. Intelligent company. The company hires me to go to make linguistics, but I will soon find that I am doing the most programming. This company signed a contract, to deliver a text analysis and extraction system running on the latest VAX computer at that time, but only has a simple system prototype written in Fortran, and it is not compiled by VAX. Standard fortran, but an old, full-supported language expansion of Fortran "dialect". Our company's residential mathematician - he started debugging these prototype systems from the company - finally decided to rewrite these code with the C language. "Why use C?", I asked. "Because", he only said such a word as concise as in the past, and he gave me a book of Kernigan & Ritchie (the translator Note: refers to the C Programming Language of the two. "
We didn't afford the VAX machine at that time, so I had a PC, which has 128K memory, and two 320K floppy drives, one drive access to the Lattice C compiler and library, and another access code. These codes can only use 64K memory because MSDOS has used additional 64K. I remember that only 4.77MHz of the 8088 CPU, and the low make people scream, it is obvious that only very small and simple languages can be running. In this way, holding a K & R book in his hand, put a C compiler disk in the floppy drive, we started working. The expression ability of C language is quickly reflected. I will easily write my code about the matrix algebra (the real programmer can use any language to do the work), and our residential mathematician will His mode matching algorithm uses intensive pointer operation to recurrent. The efficiency of C language is also fully embodied, because our code can be stuffed into the tense memory and run so far exceeds the expectations we start. When we confirm that the program is correct on the PC, we plan to transplant it to the VMS machine with two weeks, and we rent VAX machines nearby. The result was only two days, only two small mistakes were modified - because I brought the int type as 16-bit - the transplant work was completed. At the end of the second day of the university, I remember a student after I saw the screen: "What is it?"
I replied "C code."
"What is C", he asked.
"The portable structured assembly language.", I am answering him like this.
"This is what the world needs", he said.
I can't determine that I am serious or just in the end. But I was already deep, I can't fall in love with the C language.
Everything comes from needs. --anonymous
Necessity is a mother .-- anonymous
So how is this powerful tool this C language? In 1969, Ken Thompson intends to prepare a Fortran language compiler for the UNIX system created by Dennis Ritchie, Doug Mcllroy et al. On the PDP-7 machine. Ritchie recalls:
I remember that the Ken Thompson constructs the Fortran compiler 's intent for a week. And his final is a definition and compiler of a new language -B language. The B language is affected by the BCPL language, and some of the other features include THOMPSON simplified grammar and the compiler must meet very small self-volume.
Similar to the BCPL language, B language is a language that has no type of support for machine word operation, and the machine word can be used to save integers, bit mode, characters, data, and functions. The address of the maximum number of conventions in our front can be easily converted to b code:
GCD (M, N) {
While (m> 0) {
IF (n> m) {
Auto t = m; m = n; n = t;
}
m = m - n;
}
Return n;
}
Does the B language explains those "spirit" mentioned earlier? I think it interprets almost perfect. "Trust programmers" and "Do not block programmers do what should be doing things" These two principles are their own inherent characteristics of unwinding language. The 8K spatial limit and Thompson have made language concise attempts to confirm the principle of "keep language small and simple" - except for the auto modifier - that is the only white 微 微 微 微. Such a simple syntax naturally guarantees "any operation only in one way", and can only ensure the efficiency of the compiler to the rules of the machine word operation.
In this way, is it true from the language of the B language, but is it true from the "fallen" of paradise?
Just a fruit on the tree, you can't eat. - Genesis 2:17
But of the tree of the knowledge of good and evil, thou shalt not eat ... --genesis2: 17
PDP-7 is a machine addressed machine, while PDP-11 is byte addressing. Because B language is designing clumsy byte processing mechanism, the byte packaging is a major obstacle to efficiency from word decomposition. At the same time, the designer of PDP-11 promises to provide a floating point calculation unit soon, but only 16-bit word length is not sufficient to save floating point values. In order to get better performance, Dennis Ritchie decided to let go, and he introduced the type of char and float in B language. This in fact, has made compromises, violating the second and third classes of C, for the best performance, sacrificed the simplicity of the types of programming languages.
After completing this modification, Ritchie further improves the B language: provides a user-defined Struct and UNION type; the order group name is interpreted as the first address of the array in the expression; and also provides a syntax of the defined type. From the Evolution of the B language to the C language, this started this.
INT is used as the default data type, and the pointer can put into the int variable on a PDP-11, which makes the early C language compiler to compile most of the B code. Most of the assembly code for UNIX systems is also overwritten in a non-type C language. In this way, we laid two basic principles that have a profound impact on the evolution of C and C : "Programmer does not need to use things", "improvement must be compatible backward".
Natural, there is a loss. Although C is still a relatively simple language, complex type systems also bring more opportunities to generate errors, depending on programmers, actively avoiding errors, making it more difficult. Tools like Lint have gradually emerged, used to check potential types of errors; compilers have become more stringent. Therefore, the first principle of the spirit of C is also considered by people, more dependent on tools to check mistakes rather than trust the subjective behavior.
Have you doubt whether the gambling is worth it? --Joni mitchell
WERE you WONDERING WAS THE GAMBLE WORTH THE Price? - Joni Mitchell
If Dennis Ritchie is the first person eating apple, Bjarne Stroustrup is a veritable Johnny AppleSeed (Translator Note: Johnny AppleSeed is a legendary figure in the American history. He planted apples everywhere in the 19th century. Tree, laminated for forty years, today the United States is the largest apple planting country in the world to be due to him). The C language is a quasi-class security language. This is not a bad thing, but its type system is the most complicated in all languages. In the basic and derived type stem from C, C introduces reference, inheritance, multi-inheritance, virtual inheritance, virtual function and pure vanity, runtime information, function template, type template, and type derivation and other languages. Article 3 of the Spirit of C is completely lost, C sacrifices simple, and has achieved powerful expression.
Although "increasing the object-oriented mechanism" is the initial cause of C to C , the most powerful extension is indeed a generic programming capability brought by the introduction template. The introduction of the template is to provide a safe container that you can only define a class template similar to the List
Simple oak
(Translator Note: Java original development code is an oak "Oak")
Because the chaos of the chaotic is approaching the kingdom, heroes find a giant, and use force to uniform him, so that he said how to drive the chaotic secret. The giant said: Give me your left eye, I will tell you. The hero loves those people who are scratching and fear, he did not hesitate to help this. He shook his left eye to hand over the giant. The giant said: The method of controlling chaos is to use his eyes to watch it. --Jhn Gardner
Java is born in the battle of the browser of the boom. At that time, people have produced business needs of portable programming languages that can be safely compiled in an insecure browser, and people hope that this language syntax is simple and can be quickly learned at the time. So far, "Oak Tree" program, this initial project for equipment similar to cable TV set-top box, invested many resources to market by Sun Microsystems after being named Java. As already happening, Java has not made breakthroughs in the browser plugin, but is widely concerned about the field-based e-commerce server and expectation of inexpensive, simple object-oriented education.
Java's design emphasizes the third and fourth paragraph of the aforementioned "Spirit of C, sacrificing computational power and efficiency for safety and simplicity. Java is not only completely type safe, and there is no unfained behavior. This security is ensured by the language itself and Java virtual machines, so malicious code is impossible to damage the machine. In view of this, Java provides the ability to accomplish significant expression in most of the calculation. However, the exceptional situation is that people must use a more underlying C, C or assembly language to write programs for the occasions that need to fully control hardware or the most efficient use of machine resources.
So does the Java language itself high security mean that programmers are no longer trusted? At least, Java automatic memory management mechanism and cancel pointer operation means that it is impossible to appear "wild pointer" or "suspension pointer" problem. But in short, the answer to the above question is negative, here there are three examples to prove:
First, in some cases, automatic memory management actually makes the program's memory demand is more difficult to control. In the bug report received by each implementation version of the Java language, the maximum proportion is the memory leak problem of the garbage collector itself. However, most of these bug reports are incorrect, the problem is that those Java code itself holds those unwanted objects. And if you want to solve this problem, it will affect efficiency because the heavyweight object is constantly rebuilt, not always in memory. Therefore, many caching mechanisms are introduced to prevent valuable objects that are not properly released in advance, which is why the system weakreference, SoftReference, PhantomReference, Reference Queue, and Weakhashmap. Second, other resources other than memory allocated on the stack are more difficult than memory. For example, my first Java program quickly used the file descriptor:
Void Scanfile (String Name, Filter Filter) {
File File = New File (Name);
Filter.scan (file);
}
This mistake is obviously embarrassing, and this is hard but often. I will soon learn the problem with the Finally block to cope with the Quality of the Java.
Again, although there is no undefined behavior in Java, the situation of dead locks and locks is always difficult to happen. Java did not provide the language-level mechanism for preventing deadlocks to achieve concurrent control, only provided the underlying synchronous primitive, and left the responsibility of the control conflict to programmers. As early as Per Brinch Hansen Design Concurrent Pascal, a simple monitoring mechanism introduced a simple monitoring mechanism to control concurrency, 30 years have passed, and the concurrent security field has achieved a long supply. Advance, but I don't know if it is, Java is actually disappeared.
The first two examples of the above explained that the so-called Java object life cycle control is more than C . It is just a myth, let alone Java thread is more difficult to use in the UNIX system. However, although these restrictions have, Java has proven to be a successful language. It is in a surprising broad range, fulfilling procedures, and portable commitments.
Things that are full of expectations should be cautious - anonymous
Be Careful What You wish for - Anoymous
Java, as a brand new language, more refreshing from C to C . but. Despite this, the highly conciseness it brings is overhead of the speed of performance that C and C is unacceptable. So whether people want C to become simpler?
I believe that the biggest loss from elegant B language "(translator Note: refers to the evolution from B language to C and C ) to throw out non-type programming. These losses are necessary, and this loss is acceptable in the era of C, but to C , the complexity has become a scary barrier. Many complexity introduction is to support generic programming, many technologies develop templates's grammatical development over the purpose of the template initially designed. It is a bit like that is: the complex template mechanism is radically simplified.
The power of generic programming is derived from the compiler to derive the type according to the context. Extremely, this ability can even exempt the need to define the type of type. For example, a maximum code code that does not need to know the type looks like this:
Template
T GCD (T M, T N) {
While (m> 0) {
IF (n> m)
SWAP (M, N);
M = m - n;
Return n;
}
This looks not bad, but as the number of types of types have become very bad, this is almost impossible to expire the result type dependent on two or more parameter types. So I hope that the syntax of the template is no longer the requirements for grammar, but let the compiler to do this:
GCD (M, N) {
While (m> 0) {
IF (n> m)
SWAP (M, N);
m = m - n;
}
Return n;
}
This is refreshed and concise, as in the previous B language. It is also very safe because the compiler guarantees that all derived types are matched (Artima Editing: This is the working mechanism of templates in D language!)
I really can't help but list more expectations for languages, but I think it is what I will next article.
Acknowledgments for thanking Angelika Langer to comment on the first draft of this article.