Stephen King "How to write code"

Yes, that is right. Nevertheless, he has a book with such content. Although he did not think that he wrote her about it. Although the result is written. Although, probably, he does not know about it. What am I discussing here? About the beautiful book “How to Write Books” I recently read (English - “On Writing”).
King wrote it as an experienced writer for beginner writers. However, as a result he got a book telling not only how to write fantastic literature, but also how to write anything at all - including program code. After reading two dozen basic icon books of the modern software industry over the past couple of years (you know, all these "refactoring", "patterns", "clean and perfect codes, etc.), I can say that King’s book is quite a place among them, and not in last place. Then there will be a couple of examples (and spoilers as a result), so the most correct thing is not to continue reading the article, but to go and read the book itself . Then you can go back and compare your findings with mine. Well, if you don’t have time for a book, there are excerpts and conclusions under the cut.
Work, work and work again
Creator geniuses are few. King himself does not relate to them. He talks about how, from an early age (from preschool age), he began to write short stories, passages, drafts, as he showed them to different people, and sent them to magazines and publishers. And how he was footballed for a decade and a half. And he himself now admits that he wrote poorly then. These decades and a half he needed to improve. To eventually write what is sold in tens of millions. The idea that quality is the result of daily work for many years is very important for the programmer. You know, now all sorts of "23-year-old seniors" are fashionable there. Maybe some of them are really talents from nature. But what most of us write in the code for the first 5 years of our work is only suitable for the Shift + Del operation. Well, if this time is to study, internship or position of "junior developer". But sometimes people, due to pride or circumstances, seriously write production code in this way. What then they themselves and the employer pay for. There will be no miracles. In Russian fairy tales, it’s fashionable to sit on the stove for 33 years, and then suddenly become a hero or catch a goldfish. Craving for a freebie and belief in miracles - these circumstances are inherent in many. Stephen King talks about the fact that there are no miracles (although here's the paradox - most of his books say otherwise!). Until the time of world fame, he himself interrupted a penny from little-known magazines, studied as a teacher and then worked for him, and also earned money in the laundry room. And all this time he wrote, wrote, wrote. and the employer. There will be no miracles. In Russian fairy tales, it’s fashionable to sit on the stove for 33 years, and then suddenly become a hero or catch a goldfish. Craving for a freebie and belief in miracles - these circumstances are inherent in many. Stephen King talks about the fact that there are no miracles (although here's the paradox - most of his books say otherwise!). Until the time of world fame, he himself interrupted a penny from little-known magazines, studied as a teacher and then worked for him, and also earned money in the laundry room. And all this time he wrote, wrote, wrote. and the employer. There will be no miracles. In Russian fairy tales, it’s fashionable to sit on the stove for 33 years, and then suddenly become a hero or catch a goldfish. Craving for a freebie and belief in miracles - these circumstances are inherent in many. Stephen King talks about the fact that there are no miracles (although here's the paradox - most of his books say otherwise!). Until the time of world fame, he himself interrupted a penny from little-known magazines, studied as a teacher and then worked for him, and also earned money in the laundry room. And all this time he wrote, wrote, wrote. Until the time of world fame, he himself interrupted a penny from little-known magazines, studied as a teacher and then worked for him, and also earned money in the laundry room. And all this time he wrote, wrote, wrote. Until the time of world fame, he himself interrupted a penny from little-known magazines, studied as a teacher and then worked for him, and also earned money in the laundry room. And all this time he wrote, wrote, wrote.
Closed and open door
King says that he always writes at least two drafts of his works - one “behind a closed door”, the second “with an open door”. By this he understands this: the first version of the work should be dictated by the head and soul of the author. He needs to provide an opportunity to create and fantasize, write what the muse tells him to. But here is what happens as a result - in no case should you immediately give it to the reader. All this needs to be rewritten "with the door open." Those. give the story to his wife, closest friends, trusted editor or critic. As a rule, these people will find a lot of blunders, stupidities in the work, point out incomprehensible things, contradict well-known facts, etc. Many of these comments will be on the case. And then the text needs to be rewritten taking these comments into account. Look
- The programmer needs to ensure peace, silence and the absence of distractions.
- The code must pass reviews or tests before sending it to production. Well, when the code is owned by several developers.
It would seem - what different worlds: writing fantastic literature and programming. And you see - the essence of the process is the same.
Another rule that Stephen talks about is that the second draft version must be necessarily shorter than the first (well, at least 10%). And again - we all know that the best refactoring is one in which you delete the code without reducing the functionality of the system. Non-existent code does not need to be stored, read, maintained, tested, there can be no error in it. This is the best code.
Please the reader
To write what you like yourself or what the reader likes? Some authors believe that adapting to the reader means betraying yourself and becoming a “literary bedding” that can only raise money in the wake of fashion. King categorically denies this idea. He says that creativity must have an audience. If you do not want to please the reader - write to the table or even burn everything written right away. But if you decide to publish, you need to think about the public. What will be interesting for her and what is not, what is understandable, and what is difficult, what will cause delight, and what will cause boredom? All these thoughts should lead the pen of the writer. Otherwise, he is not a professional, but an amateur. Otherwise, you can immediately sign up for "unrecognized geniuses" and leave with a supply of clean paper to live in the taiga. How often is this lack of understanding for programmers! After all, we do not work for ourselves, not for bytes in the computer’s memory, but for the end user. It should be convenient, useful, fast and comfortable for him with our program. It is his problems that we solve, not ours. He is our employer. If you write for the sake of yourself only - you can call it a hobby, a hobby, but certainly not the work of a professional.
Rhythm
Stephen says that opening any book anywhere can be very likely to tell about the degree of its quality, even without reading the text. You can immediately see how words form sentences, how long paragraphs are, how much space is occupied by dialogs, formulas, quotes, etc. This all gives an understanding of the complexity of the book, its style, dynamics, etc. Even a quick glance at 2-3 pages, it becomes clear whether it will be interesting for us to read this book. Exactly the same goes for code. Opening any source, we already feel its spirit - you can immediately see the sizes of functions, the average length of lines, the number of methods of the class, the presence of comments, tests, etc. I don’t need to read the code completely to see its rhythm, 2-3 pages will already give an understanding of whether it is worth using the library in my project. As for the author of the code, this gives me an understanding of what the user expects from me.
Set of tools
No matter how good a master is, it is not convenient for him to work without a suitable tool. Several chapters in the book are devoted to choosing the right writer tools. The author discusses the need for both trivial tools (such as a suitable text editor, dictionary, reference books), and higher-level ones - such as personal rules for naming characters, avoiding literary cliches, prohibitions on certain words, and the correct construction of sentences. For programmers, of course, the “lower echelon” of the toolkit consists in a suitable hardware and software, correctly selected plugins for your favorite IDE, learned hotkeys. The “upper” level of tools is more complicated - these are proven libraries (one's own and others'), the right patterns, this is in general experience and accumulated intuition, which tells which solution will work and which will not. Creating all this toolkit is a very important part of the programmer’s work. Without it, he will be like a caveman trying to make fire with his bare hands.
Extract artifact from the breed
I heard this idea not only from King - they say it was easy to create this masterpiece, it always existed somewhere, like a marble statue was always in a piece of solid stone before its creation. The most difficult thing is to extract the artifact from the "breed" without damaging it and not capturing excess garbage. The thought is very beautiful. Sometimes you look at a program and see it just like such an artifact - everything in it is in it. Just think, “It would be nice if there was such a function here” - as you look, but it is there. And that’s how it works, as you thought. The way of thinking of the author of such a program is probably very similar to the way of thinking of the creator of the art. To be able to extract such pearls is a great skill, it is worth striving for it.
Literary agent
To be able to write well (code or books) is half the battle. You need to bring your product to the consumer, find someone who will be willing to pay money for it. In the publishing world, there is such a profession as a "literary agent" - a person who represents the author before publishing houses, determines the size of fees, solves copyright issues and, in general, mutual obligations. In the IT world today there are so many different types of cooperation (both direct and through intermediaries). Some of them entrust a huge pile of non-programmer work to the programmer, while others leave room for pure creativity. As for me, it’s better to concede a couple of percent of your salary for the sake of being able to do only what you like, but for someone it could be vice versa. No wonder there is still a heated debate on the subject of "freelance vs regular work vs your startup vs something else." Something suits everyone, the main thing is not to be lazy to look for it and find it.
The author writes about all this and much more much more interesting and colorful than me, the tongue-teller reteller, so even if you read all the above spoilers, it still makes sense to read the original book. I think you will be interested.