Programming Paradigm-Structured Programming

This series of articles will introduce the three main programming paradigms in turn, which are structured programming, object-oriented programming and functional programming. This article focuses on structured programming.

unstructured programming

Unstructured programming is the earliest programming pattern in history capable of creating Turing-complete algorithms. Unstructured programming languages ​​include both high-level and low-level languages. Some languages ​​commonly identified as unstructured include JOSS, FOCAL, TELCOMP, assembly language, MS-DOS batch processing, and early versions of BASIC, Fortran, COBOL, and MUMPS.

The most criticized aspect of unstructured programming is that it produces code that is difficult to read (jokingly called spaghetti code). It is sometimes considered poor at creating large projects. However, because it gives programmers a lot of The freedom has been praised as being like Mozart composing music.

Feature 1: Line number and GOTO

A program using an unstructured language often contains a sequence of commands or statements, usually each occupying one line. Each line is numbered or labeled so that any line in the program can be executed.

In unstructured programming languages ​​such as BASIC, line numbers are also used to indicate the destination of branch instructions. For example:

10 IF X = 42 GOTO 40 '最前面的"10"是行号,X=42时会跳到行号=40的程式码,否则会跳到下一行
20 X = X + 1
30 GOTO 10'跳到行号=10的程式码
40 PRINT "X是42!"'输出"X是42!"

Goto-style branching is generally considered to be a poor programming style because it tends to form complex and difficult-to-understand spaghetti code.

Languages ​​such as C and C++ still retain the GOTO instruction, but the destination of the GOTO has been marked with a marker instead. This type of GOTO instruction is generally not recommended to be used. It is only recommended to be used in a few situations, such as jumping out of multiple loops:

while(1) {
  while (1) {
    if (done) {
      goto freedom;
    }
  }
}
freedom:

The above program can be compared with the following program that does not use GOTO. The following program needs to be judged based on the value of the done variable at different positions. In comparison, the above program is more readable. It also proves that goto statements can be rewritten as non-goto statements.

while(1) {
  while (1) {
    if (done) {
      break;
    }
  }
  if (done) {
    break;
  }
}

In addition to C/C++, some people will point out that the named break statement or Exception in Java is very similar to goto. In fact, these grammatical structures are not at all the same as the completely unrestricted goto statements in older programming languages ​​​​such as FORTRAN and COBOL. Even those programming languages ​​that still support the goto keyword usually limit the target of goto to not exceed the scope of the current function.

Feature 2: Spaghetti Code

Spaghetti code is a type of anti-pattern in software engineering. It refers to the control flow of source code that is complex, confusing and difficult to understand, especially when many GOTOs, exceptions, threads, or other unorganized branches are used . The reason for its name is that the flow of the program is as twisted and tangled as a plate of noodles.

On the one hand, it is very easy to write spaghetti code when using various assembly languages ​​(and their underlying machine languages) or scripts. The reason is that these low-level languages ​​rarely have mechanisms that can correspond to FOR loops or WHILE loops.

On the other hand, even in the more popular high-level languages ​​now, it is easy to form spaghetti-like code due to programmers with no experience or craftsmanship, or complex programs that have been modified frequently for a long time.

for i in [1,2,3]:
    def printMa():
        print ('Ma')
    x = True
    if x == True:
     printMa()
    y = False
    if y == True:
     printMa()
    else:
     print("Ma")
    y = True
    if x and y == True:
        if i == 3:
            print('Mia let me GO !')
        else:
           print ('Mia')

Spaghetti code for high-level languages

structured programming

Structured programming, a programming paradigm. It uses subroutines, block structures, for loops, while loops and other structures to replace the traditional goto. The hope is that this will improve the clarity, quality, and development time of computer programs and avoid writing spaghetti code.

some interesting history

Dijkstra came to the conclusion early on that programming is a difficult activity. A program, no matter how complex it is, contains a lot of detailed information. Without the help of tools, this detailed information is far beyond a programmer's cognitive ability. In a program, even a small detail error can cause the entire program to go wrong.

The solution proposed by Dijkstra is to use mathematical derivation. His idea is to draw on the axioms (Postulate), theorem (Theorem), corollary (Corollary) and lemma (Lemma) in mathematics to form a Euclidean structure. Dijkstra believed that programmers could reason about their programs just like mathematicians.

axiomatic system

The axiomatic system was originally established by Euclid with the purpose of establishing a rigorous theoretical system through a standardized process. Simply put, an axiomatic system is to select a set of self-evident and basic axioms, and then use these axioms as the cornerstone to prove and derive other key theorems. From this, a complete mathematical theory system can be formed, such as the Euclidean geometry theory established by Euclid. The axiomatic system has three important properties, namely, self-consistency, independence, and completeness. An axiomatic system with such properties is a good axiomatic system.

In other words, the programmer can string together some proven constructs with code, and by proving that the extra code is correct by himself, he can deduce the correctness of the entire program. Of course, before that, we must first show how to derive and prove the correctness of a simple algorithm, which itself is a very challenging task.

Developed in the 1960s, Corrado Böhm and Giuseppe Jacopini published a paper in the journal "Communications of the ACM" in May 1966, proving that one can use sequential structures, Conditional structures and loop structures can be used to construct arbitrary programs. It also means that any program with goto instructions can be changed to a program that does not use goto instructions at all.

 Sequence, branching and looping structures

This discovery is very important because it proves that the set of control structures we need to build deducible modules is equivalent to the minimum set of control structures required to build all programs, and thus dependent structured programming was born.

Dijkstra discovered a problem during the research process: certain uses of goto statements will cause a module to be unable to be recursively split into smaller, provable units, which will result in the inability to use decomposition methods to further split large problems. into smaller, demonstrable parts.

Dijkstra proposed the famous paper "Go To Statement Considered Harmful" in 1968, so structured programming became popular. However, goto also has strong supporters. All in all, this heated debate lasted for more than 10 years. Today, we are all practitioners of structured programming, whether willingly or not, because our programming languages ​​basically prohibit unrestricted direct control transfer statements.

essential thought

The essential idea of ​​structured programming is that the direct transfer of program control is restricted and standardized. Using unrestricted jump statements such as goto will damage the overall structure of the program. It is advocated and proved that all programming can be programmed with sequential structure, branch structure and loop structure.

structured programming

Dijkstra proposed the famous paper "Go To Statement Considered Harmful" in 1968, so structured programming became popular. The core idea is to recursively decompose a complex software engineering project into deducible and provable functions. Each split function can be written using a structured programming paradigm.

This article does not expand on programming patterns, and will explain them in separate chapters on structured program analysis and design.

Guess you like

Origin blog.csdn.net/u011365893/article/details/129855595