📜 ⬆️ ⬇️

Digging just down

Hello, dear readers.

Recently, we were interested in the Zed Shaw series " The Hard Way ", which I would like to at least partially translate into Russian. Since sometimes we are not really looking for easy ways, I would like to start with a book on the C language:


')
The series is focused primarily on beginners. For those who love the C language, as well as for their opponents who believe that it is better to start with something simpler, we publish a slightly abridged article by Evan Miller, written late last year. Perhaps, depending on the reaction to this article, we will decide to further translate and publish an excerpt from Mr. Shaw’s book, or even his response to the criticism expressed by Tim Hentenaar , but for now we invite you to the cat, where, we believe , the most general considerations are presented in favor of the relevance of this book.

When I was learning to drive a car (I was then just over 15), my mother insisted that I practice on my father's Mazda Miata: it was a silver two-door convertible with a convertible top.

Managing Miata was not easy. Not only did it not have an automatic transmission, in addition from this car it was almost impossible to look back. The sun so fried plastic rear window, that on a rolled-up top, it seemed, it was possible to prepare sandwiches.

Miata was by no means the safest car, and Mazda Motor Corporation did not even try to say the opposite. It was just an affordable car, which is nice to ride, such a budget BMW. I remember my father's Miata was so narrow and squat that when I drove it along the federal highway, even thoughts crept in: “And if I try to slip on her under the semitrailer, the trucker will notice something”?

We also had a minivan and SUV - larger and more reliable cars with transparent rear windows. Many parents would prefer to teach teen driving it on them. But not my mom.

“One day you will be at the party,” she explained to me with confidence, which Nostradamus would envy, “and there will be a car that no one can drive because she will have manual gear shifting. And you should be able to, and you need to learn it now, otherwise you will never cope with the shift knob. ”

Mastering the manual gearbox is not the most interesting thing, and I think that I would never do it if it were not for the prophetic authority that my mother used at home. But, having practiced, I was eventually able to ride the Miate within the permitted speed, without filling the driver's seat with sweat and tears.

Many years have passed since then, and I noticed an interesting pattern. We, the proud minority — the hardened hand-over masters — turned out to be much more skilled than those who were used to relying on the automatic transmission. We confidently owned the car, better imagined how the car engine works. Even if we never had a chance to look inside the gearbox, we knew what an " overdrive " in an automatic box was, how to save gasoline with it. We knew what the mysterious numbers 1 and 2 mean under the inscription “Drive”, what kind of service they could do when moving down the slope. And at each moment in time, we probably, at an intuitive level, had a better idea of ​​what was going on with the car — better than adherents of an automatic box.

I don’t regret having mastered the manual transmission at one time, although I didn’t have the chance to bring substantial benefits to society — for example, I never drove an ambulance in which orphans would sit under fire. Just manual gear shifting is a good skill, it won't bother anyone. I think that the more drivers will own it - the better.

Dear reader, I did not write all this in order to advertise myself as an excellent driver for low-budget convertibles, and not to oppose the dominance of automatic transmissions in the automotive industry. In fact, I just see the important parallels between the proliferation of such boxes in the automotive industry and the Python language in programming.

Python is convenient, in many respects it greatly exceeds the language of C. But if you teach a teenager to ride an automatic transmission, you can be almost sure that he will not even think about switching gears. By advising neophytes to learn Python, we breed neophytes who will never try to write C code. I think this is bad.

Expressing this opinion, I find myself in the minority, so I especially want to explain here my point of view. Not so long ago, Python pressed Java as the most popular introductory programming language studied in universities. Eric Raymond, author of the How To Be a Hacker article, recommends Python as the first language. He is certainly supported by Peter Norvig in his famous work “Teach Yourself Programming in Ten Years”. Paul Graham even talks about a specific "Python Paradox".

Nowadays, if you see an ad in the spirit of “How to learn how to program in X weeks,” you can be almost sure that you will be offered to learn Python.

Contrary to the superficial appeal of Python — its syntax, interactivity, library support, the wider community, and excellent documentation — by choosing it as the first programming language, you face a serious problem. A programmer who knows only Python does not have a sufficient understanding of computer operation. He may perceive C as a mystical art, causing mixed feelings of fear and dislike. Fear - because the programmer was told that C is complex, and hostility - because the complexities of C are supposedly outdated remnants of more ascetic times. There was even a new truism: "human time is more valuable than processor time."

But Python and C are not just alternatives to each other, where one language is a slower and more convenient replacement for another. Python also cannot be considered unconditional technological progress in comparison with C. It is better to perceive Python and C as languages ​​coexisting in independent parallel sections of reality, where C is one level closer to the machine.

Paul Graham picked up a beautiful metaphor (as always, right?) About the potential of programming languages, which he describes as a kind of vertical continuum. Languages ​​with minimal capabilities are located at the bottom, with the maximum - at the top. Somewhere in the middle is the hypothetical language Blub, which slightly less than fully resembles C. Graham believes that Blub users will despise all the languages ​​that lie below Blub in this continuum — Cobol, machine language, etc. - and simply will not be able to understand the possibilities of those languages ​​that lie above.

This is a great image, but let's look at this metaphor from the other side. Perhaps Python is above C, and Lisp is like the cherry on a cake of abstractions, but learning Python or Lisp doesn’t help too much to understand how the computer on which this continuum rests works. Thus, despite the fact that Lisp programmers seem to be omniscient celestials, in the hierarchy of programming languages, looking “down” is just as easy as “up” .

In fact, I would even say that looking down is more difficult than up. A pure Lisp boxer reviewing C code sees only nasty syntax, unfamiliar functions and mysterious comments about the cache lines. A C-programmer, looking up, sees a whole bunch of things implemented in C.

The popularity of Python is due to the fact that it occupies a position above average in the described continuum. It is more abstract than C and more productive in work, but not so abstract that your head is spinning from metaprogramming and recursion.

The trouble is that, just as Lisp seems fanciful and redundant to the average Blub programmer, so does C produce a similar impression on the clowner. What are these pointers everyone talks about? "It sounds surprising (thinking the coffer) and if I still somehow work without these pointers, I will do without them further."

Therefore, for the most part, the python team will never study C.

Undoubtedly, you can make a long and successful career in IT without knowing C. However, the programming culture without studying C gives a number of perverse effects that are directly related to the fact that a person becomes familiar with programming using the example of Python.

First, fewer and fewer people are able to write excellent programs. To program well — for PCs, mobile devices, alien ships — you must clearly understand how your computer and operating system function at the lowest level. How many data structures fit into the processor cache? Why is the code slow and does the built-in profiler say it’s fast? When I order the operating system to do something, what does it really do? I think all these things can be memorized, but nothing helps to acquire the necessary intuition, like writing and optimizing a large amount of code in C.

You may argue that at a given historical moment it is more important to write more programs than more quality programs. Perhaps from an economic point of view it is. Technologies are developing very quickly, there is no need to write a filigree worked program that nobody needs. But the culture of “just use Python”, “computers are fast enough now” and “let the problem solve iron” lead to a deep systemic crisis: when they enter the profession through Python and criticize C, fewer and more clever people will fall into professional programming.

Moderate anger for a moment and listen to my arguments.

I believe that for such "clever men" who are going to learn programming, Python as the first language is unattractive for two reasons. First, learning Python is not so difficult. The key concepts of this language do not cling to technical intellectuals and, accordingly, do not arouse much interest. Learning Python, I would say, is somewhat boring. It's like going to school, where there are no core classes.

The second mechanism was described by my friend, who recently signed up for a Python workshop. She said that with great pleasure she taught cycles and functions. They were small, understandable, self-sufficient. But then the teacher decided to demonstrate "the full power of Python" and spent the rest of the class writing a useless program with the Python Twitter API.

Handling libraries is an important skill; any professional developer will confirm this. However, if it is premature to get acquainted with libraries, then they erect a whole wall of abstractions, at the sight of which smart people feel just aghast. So my friend thought she was about to learn the secret laws governing computers; instead, the teacher simply rolled out in front of her a monolithic user application with a completely impenetrable abstraction of the client library. Not having mastered, above all, the language — and not having learned to read the source code of this library — my acquaintance was left without tools with which to dig deeper and understand what was happening at the bottom of the call chain.

“Just got a bad workshop,” you say. But with this example, I want to emphasize a larger problem: not having studied C for the beginning, the programmer is deprived of the necessary tools to understand exactly what is happening in the system used . If you are an intelligent and inquisitive poffer, then you will soon get to the dense rocks of the C language. Under these horizons, they will tell you, “be afraid of dragons, bones and debuggers”. Accordingly, if you are not brave enough and do not ignore the warnings “don't take it for this C,” you never explore the depths that you can climb just out of curiosity.

On the contrary, sishnik trying to understand a piece of code in C, in the worst case, will have to shovel a bunch of header files, man-help and source code. But he is unlikely to come across some strange Lisp or Python package. Always dig down, not up . That is the essence of computers.

**
The programmer’s motivation is twofold: either solve the problem as best as possible, or understand how certain things work. Undoubtedly, Python is great for solving problems. This is evidenced by the popularity of Python on startups and university courses (including Data Science). If you are going to learn only one programming language in your life, then Python is a reliable, efficient and reasonable choice.

But Python is completely unsuitable for those interested in how a computer works. This is a problem, because in the long run, the best programmers come from people who are overcome by knowledge. Perhaps in the short term, these are not the best employees - they read books at work, introduce some senseless optimization - but, having passed a sufficient number of rabbit holes, they can draw you a real map of wonderland.

This, I believe, is the real problem with "programming without C" - and also with the more global idea that introductory programming languages ​​should be "expressive" and "accessible to beginners." The guild of programmers loses just those people who could decorate it with them.

I want to emphasize that I do not mean people who want to combine programming with the main work (scientists, journalists, web designers, etc.). I'm talking about those who are interested in programming as such and think that one day they could succeed in this.

In short, I’ll dwell on why Python is taught at universities (and previously taught Java). The main reason is that the task of universities is not to educate aces programmers. At most faculties, they try to simply prepare as many successful graduates as possible who will go to graduate school or find a well-paying job. Neither requires an exceptional programming prowess.

From the teacher’s point of view, a programming language is the material on which you need to learn the basics of computer science: algorithms, data structures, etc. Python is the golden mean, because it is easy to study (professors are satisfied), and it is also used in commercial projects (professors are simply jubilant). Add to this the popularity of Python in universities, the abundance of online materials, and the widely quoted (and rarely disputed) allegation that Python is a “good learning language.” So what can go wrong with him?

It seems to me that people who get into programming through Python miss a lot of interesting things. There is a widespread belief that the buzz of programming is to solve various tasks in a minimal amount of code, using client libraries, etc. with might and main. This is true in cases where a person must cope with the work, meeting the deadlines. But if your main motivation is to learn something and become a first-class programmer, then it will be much more interesting to write programs that do not work as effectively but leave food for thought.

For people who have never programmed before, the C language is a great simulator. Pointers and their arithmetic have no close analogues in the everyday world. By learning this language, you will stumble upon seemingly meaningless error messages and deal with non-deterministic failures. If you have problems with arithmetic, then you can damage the call stack; forget about the simple verification of boundary conditions - and the heap will crumble like a house of cards. Sometimes you want to break the monitor or porydat on the keyboard.

But the matter is worth it: the C language helps to get a good idea of ​​what exactly the computer does. You will feel like a teenager battling with a manual gearbox. By asking the questions “why?” And digging deeper, you will gradually understand the process model, processor architecture, memory hierarchy, understand the operating system, etc. It is this mental model, and not the C language as such, that will help you navigate abstractions that others have written and create programs that once seemed unthinkable to you.

So, if you want to learn how to masterfully program - in any language - then do not be persuaded to learn Python or, God forbid, Lisp, but begin your journey with C. There are gorgeous views from above, but if you want to conquer the mountain, then climb it better just below.

Source: https://habr.com/ru/post/271347/


All Articles