📜 ⬆️ ⬇️

Stephen King "How to write program code"

"What? Stephen King "How to write program code"?! There is no such book! Yes, he is not a programmer at all! ” .
Yes, that is right. Nevertheless, he has a book with such content. Although he did not think he wrote her about it. Although the result is written. Although, probably, he does not know about it. What am I talking about here? About the beautiful book “How to write books” that I recently read (eng. - “On Writing”).

King wrote it as an experienced writer for beginner writers. However, as a result, he got a book that tells not only how to write exactly fantastic literature, but also how to write anything at all — including program code. After reading over the past couple of years two dozen of the main books-icons of the modern software industry (you know, all these “refactorings”, “patterns”, “clean and perfect codes”, etc.) I can say that this book is quite to yourself place among them, and not in last place. Next will be a couple of examples (and as a result - spoilers), so the most correct would be 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


Geniuses are few creators. King himself does not consider himself to them. He talks about how from an early age (from preschool age) he began to write small stories, excerpts, sketches, as he showed them to different people, sent to magazines and publishing houses. And as his fifteen years football. And he himself now admits that he wrote then rather weakly. These one and a half decades he needed to improve. In order to finally write what is sold in tens of millions. The idea that quality is the result of everyday work over the years is also very important for a programmer. Well, you know, now 23-year-old seignoires are fashionable there. Maybe some of them are true talents from nature. But the fact that most of us write in the code the first 5 years of our work is only suitable for operation Shift + Del. It is good if this time is spent on studying, internship or the position of a “junior developer”. But sometimes people, by virtue of pride or circumstances, quite seriously, write the production-code like this. Then what are they paying for and the employer? Miracles will not happen. In Russian fairy tales, it is fashionable to sit on the stove for 33 years, and then suddenly become a bogatyr or catch a goldfish. Traction to freebies and faith in miracles - these circumstances are inherent in many. Stephen King says that miracles do not happen (although here's a paradox - most of his books say the opposite!). He himself, to world-wide fame, was interrupted by pennies from little-known magazines, studied as a teacher and then worked with him, and also worked in a 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 the closed door”, the second “with the door open”. By this he means 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. But here's what happens as a result - in no case can not immediately give the reader. All this needs to be rewritten "with the door open." Those. give a story to read to his wife, closest friends, trusted editor or criticism. As a rule, these people will find in the work a lot of blunders, nonsenses, point out incomprehensible things, a contradiction of well-known facts, etc. Many of these comments will be on the case. And then the text needs to be rewritten taking into account these comments. Look at how well this thought is in harmony with facts well known in the IT world:
  1. The programmer needs to ensure peace, silence and the absence of distractions.
  2. The code must be reviewed or tested before being sent to production. Well, when the code is owned by several developers.

It would seem - what are the different worlds: the writing of fantastic literature and programming. But you see - the essence of the process is the same.
Another rule that Steven speaks about is that the second draft should 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 code without reducing the functionality of the system. Non-existent code should not be stored, read, maintained, tested, there can be no error in it. This is the best code.
')

To please the reader


Write what you like most or what the reader likes? Some authors believe that adapting to the reader means betraying yourself and becoming a “literary litter” that can only raise money on the wave of fashion. King categorically denies this thought. 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 clear and what is difficult, what will cause delight, and what is boredom? All these thoughts should lead pen 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 blank paper to live in the taiga. How often this understanding is not enough for programmers! After all, we work not for our own sake, not for the sake of bytes in the computer's memory, but for the sake of the end user. This should be convenient, useful, quick and comfortable for him with our program. It is his problem that we solve, not ours. He is our employer. If you write to please yourself - you can call it a hobby, a passion, but certainly not a professional work.

Rhythm


Stephen says that by opening any book anywhere you can with a high probability say about the degree of its quality, even without reading the text. We immediately see how the words are put together into sentences, how long the paragraphs are, how much space dialogues, formulas, quotes, etc. take. This all gives an understanding of the complexity of the book, its style, dynamics, etc. Even glancing over 2-3 pages, it becomes clear whether we will be interested in reading this book. Exactly the same goes for the code. Having opened any source code, we already feel its spirit - the sizes of functions, the average length of lines, the number of methods of a class, the presence of comments, tests, etc. are immediately visible. I do not need to read the code completely to see its rhythm, 2-3 pages will already give an idea whether to use the library in your project. As for the author of the code, this gives me an understanding of what the user expects of me. I'd like to disgrace myself in front of him.

Set of tools


No matter how good the master is, without a suitable tool, he is not comfortable working. Several chapters in the book are devoted to choosing the right toolkit for the writer. The author argues about 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, banning certain words, and building sentences correctly. For programmers, of course, the “lower echelon” of the toolkit consists of suitable hardware and software, properly selected plugins for your favorite IDE, learned hotkeys. The “top” level of tools is more complex - these are proven libraries (own and others), the right patterns, this is in general experience and accumulated intuition, suggesting which solution will work and which one will not. Creating all of these tools is a very important part of the programmer’s work. Without it, it will be like a cave man trying to make fire with his bare hands.

Remove 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 that, as a marble statue was always in a piece of solid stone before it was created. 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 some program and you see it exactly as such an artifact - everything is in its place. Just think "it would be nice if there was such a function here" - as you look, and it is there. And this is exactly what 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 an object of art. To be able to extract such pearls is a great skill, it is worth striving for it.

Literary agent


Being able to write well (code or books) is still 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 to publishers, 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 put a huge pile of non-programmer work on the programmer, others leave room for pure creativity. As for me, it is better to give up a couple of percent of your salary for the sake of being able to do only what you like, but for someone it can be the other way around. It is not without reason that there are still heated debates on the subject of “freelancing vs normal work vs your startup vs something else.” Everyone has their own suit, the main thing is not to be lazy to look for it and find it.

The author writes about all this and many other things which is more interesting and more colorful than me, the tongue-tied talker, so even if you read all the above-mentioned spoilers, it still makes sense to read the original book. I think you will be interested.

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


All Articles