Exit points or a little about structured programming

    When reading Habrahabr or looking at other people's source codes, I quite often have to notice about the following piece of code that “sounds” exactly the same in any language, no matter what it is written in:
    function (single_document)
    {  
      if (single_document.getElementById ("comments")! = null)
        return;
        ...
        ...
        ...
        ...
    } * This source code was highlighted with Source Code Highlighter .

    Here's a piece of Javascript code , but the same thing can be written in dozens of other languages. What is wrong here? Only that a function (method, property, procedure) has several exit points. If you are wondering why this is bad, read what is written under the cut.

    Of course, each programmer has his own style, which imposes character on the written code. A sloppy programmer writes sloppy, tidy sticks to the rules and writes code that is nice to read. And not only read, but also support to accompany, change. On the other hand, programming paradigms are changing dramatically and are now described by Dijkstra , Knut and Wirth.the principles are a little outdated, sometimes they don’t fall under the format, in places they’re simply not applicable. But, the fundamentals, the foundation of structural programming will always remain, while the practice of “divide and conquer” is still alive.

    Thank God, you can hardly find the use of the goto operator in the code , although this is also the case . But more often, many other less obvious principles of structural programming are not respected . One of these principles, the authorship of which is attributed to Edsger Dijkstra , is:

    “A module (in this case, a function) should have only one entry point and only one exit point”



    In this regard, the use of the return operator in the above example contradicts the principle of structural programming. What is so good about this principle?
    The reasons for adhering to this principle are as follows:
    • the complexity of debugging such code is not always obvious when a return from a function occurred, for example, we have several return and we can never say for sure which one was called;
    • problems with maintenance and change. When looking at the code, especially if it is large, existing exit points are not always noticeable. In this regard, the behavior of the added code can be unpredictable, it is not known for sure whether your code added to the end of the function will be executed or not. In order to be sure of this, you need to unambiguously evaluate all exit points, understand the logic, and it is possible to implement a call of your code in front of each exit point.

    The two reasons presented are enough for me personally to abandon the simple solution of interrupting function code in several places. Experience has shown that the habit of reducing code to the above-mentioned principle of structural programming is only beneficial, does not limit the programmer in any way and contributes to writing more readable code.

    How to rewrite the presented code:
    function (single_document)
    {  
      if (single_document.getElementById ("comments") == null)
        {
         ...
         ...
         ...
         ...
        }
    } * This source code was highlighted with Source Code Highlighter .

    The presented code is minimal in meaning and content, you need to understand that it is intended to show the principle, and not demonstrate the result. Many may say that a single return from a method or function at the very beginning of the code is common practice and that there are no problems and that is constantly being used. I will not dissuade, but I would like to recall that the principles are principles and that they are always adhered to, and not only upon request. Firstly, it educates, and secondly, it improves in some way the code, which will always be built on the same principles, without violations. And thirdly, the habit of writing correctly is what everyone should strive for, isn't it?

    Also popular now: