📜 ⬆️ ⬇️

The book "Programming without fools"

image Perhaps you are well aware of the problems in your code. You may even have an idea of ​​what you need or not need to do in the future to become a good programmer. “There’s nothing wrong with being a bad or average programmer,” writes Steve McConnell in his book Code Complete. “The only question is how long a programmer can be bad or average, not realizing what can be done better.” Why is it often so difficult to go to the stage of identifying problems and finding ways to fix them?

The main reasons that we find the wrong ways to solve problems lie in the plane of our human qualities. In the first place in this case is conservatism. At first, it is not so easy to resist a strong desire to do everything the same way as before. The brain has to economically allocate its resources and therefore functions in such a way as not to reject an acceptable solution to the problem at the sight of a vague alternative on the horizon. And the study of all current trends in new technologies, languages, methods and frameworks is too time-consuming event that can make you forget about a fun life.

While children and adolescents learn something new most of the time, adults often do not improve their knowledge over a long period. “Well, yes, but it has always been like that,” a bad programmer would say, who has no desire to even imagine how to make the world better and not spend the whole day changing all lowercase letters in the text to uppercase letters and also arrange point over "E". He does this not only because he wants to use a proven solution to a problem, he also wants, for quite understandable reasons, to spend as little time as possible on thinking about the problem. If you do something without thinking about it, you will have to go to the goal for quite a long time, although with each step, visible results are achieved. If you first think about the problem, then it can be solved in the end rather quickly, but in the first hours, days, weeks of thinking, you should not expect a visible result. Such unwillingness to invest their own resources in self-education becomes permanent, although the results in the most paradoxical way turn out to be the result of hard work. At the same time, a programmer who takes a long time to think about a problem resembles a person who is constantly sitting on the balcony and fixing a thoughtful look at the clouds or browsing the web all day long.

Chapter 19 Do Not Do It Yourself


Inexperienced programmers spend a lot of time re-inventing functions that already exist in their language or its standard libraries. Of course, at first it is impossible to get an idea about all the functions of a particular programming language. No one will start learning a language by memorizing all its commands in alphabetical order1. Nevertheless, how can you effortlessly determine where it is advisable to hover over the task and program yourself, but where do you just reinvent the bicycle (with triangular wheels)?
')
Here is an incomplete list of PHP commands that I unknowingly wrote myself:

array_rand, disk_free_space, file_get_contents, file_put_contents, filter_var, htmlspecialchars, import_request_variables, localeconv, number_format, parse_ url, strip_tags, wordwrap 

Catherine

However, inept programmers do not use existing solutions not only because of inexperience or ignorance. Even if the task posed, the programmer has a timid suspicion that someone could already face it and find a solution earlier, the next logical step is not always done: I will find this solution. Many people perceive programming as an interesting and fascinating activity, so they are much more willing to write code than they are looking for already written. In addition, people tend to overestimate their own abilities, as well as underestimate the complexity of their task. Just those tasks that seem quite foreseeable turn out to be problems, although an inexperienced programmer, having familiarized himself with the task, almost immediately believes that he has found, if not a solution, then the path to it.

Attentive readers recognize in these problems the downside of the important programming virtues listed by Larry Walls (we talked about them in Chapter 2): laziness, impatience, and reassessment of one's own strength. These qualities of a programmer become advantages only when a specialist begins to use them to the point. The one who independently writes all his tools, without being bothered by preliminary research, misses the time that could be spent on creating something really useful, which does not yet exist. After all, the first working version of the own date calculation function, the engine for a blog or a tool for working time is written surprisingly quickly, but you just start using it, as some special cases, bugs, suggestions for expansion start to appear ... And when with code other people start working, his support can take a huge chunk of life away from you. According to a well-known rule of thumb, an experienced programmer, considering thinking, testing, debugging, optimization and documentation, writes about ten lines of perfected code per day (by the way, the situation for writers is similar). An inexperienced developer will probably do no more.

To solve the ever-recurring problems, there are standard solutions brought to perfection by many generations of programmers. These are, for example, sorting algorithms. The need to work on a task that requires a special, not previously existing sorting principle is in itself unlikely for an average programmer. If the standard solution offered in the framework is not ideally tailored to a specific situation, this is not a reason to develop your own solution, because in this case, you not only waste time, but also probably make mistakes, and also work contrary to the framework. In other words, as a result, you implement for yourself an excellent sorting algorithm, but inconsistencies between the code and the framework will appear somewhere else.

It’s also advisable to use ready-made solutions because home-made tools will not please those who in the future will have to work with your code and read it. Perhaps these experts will want to improve the performance of the application or find errors in it. If instead of calling the proven standard functions, it will pop up your own decision, then the unfamiliar code will cause a fair distrust. The reader of the code will have to independently deal with the details of the strange sorting function in order to make sure that it does not contain errors and really effectively solves the problem. On the contrary, if the standard function were used in the code, then an experienced reader could not do all this, since the implementation of the standard function has been tested so many times that there is no doubt: there are no errors in it and it works efficiently.

What to do


If you catch yourself writing the same code again and again to solve a relatively simple task, most likely there is already a ready-made compact solution for such a case. There are a number of possibilities to find such a solution.

- Read the documentation for the language used in this subject area. It may be possible to find cross-references to the desired solution.

- You look at the list of all functions or functions from a certain thematic area and hope that the necessary function will have a speaking name. So, the array_rand function from the example above is easily found in the PHP documentation on php.net in the “Array Functions” section.

- Verify with the book, which lists the standard solutions. For this, well-suited compilations of recipes from the publisher O'Reilly (in the original, these books are called Cookbook). They formulate typical questions arising from programming, the answers to which are given in the form of recipes.

“You’re filling a problem with a search engine and looking for a solution on services such as stackoverflow.com , where many users at once offer solutions that are more elegant than one. For example, I stumbled upon the array_rand function, setting the query “how to” “random element” array php.

- You go to github.com or sourceforge.net and search in descriptions of free projects written in the language of interest to you. There is a huge chance that your problem has already been solved. Then you find out how the project authors coped with the task.

- When you accumulate some experience, you can imagine what functions are available in a comparatively new language. It remains to clarify their names and technical details. It is not necessary to formulate in the search engine any heavy query like “how to” “random element” array php. Instead, just ask: array_rand in python to find the Python equivalent of the familiar PHP command.

Only conclusively making sure that there is no ready-made code anywhere that can be used, it is worthwhile to start programming yourself. At the same time, the source code written in another language can be very useful - with its help you will be able to at least assess the true scale of the task before you.

»More information about the book can be found on the publisher's website.
» Table of Contents
» Excerpt

For Habrozhiteley 25% discount coupon - Comp

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


All Articles