All Topics  
Structured programming

 

   Email Print
   Bookmark   Link






 

Structured programming



 
 
Structured programming can be seen as a subset or subdiscipline of procedural programming
Procedural programming

Procedural programming can sometimes be used as a synonym for imperative programming , but can also refer to a programming paradigm based upon the concept of the procedure call....
, one of the major programming paradigm
Programming paradigm

A programming paradigm is a fundamental style of computer programming. . Paradigms differ in the concepts and abstractions used to represent the elements of a program and the steps that compose a computation ....
s. It is most famous for removing or reducing reliance on the GOTO
GOTO

GOTO is a statement found in many computer programming languages. It is a combination of the English words wiktionary:go and wiktionary:to....
 statement
Statement (programming)

In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program is formed by a sequence of one or more statements....
.

Historically, several different structuring techniques or methodologies have been developed for writing structured programs. The most common are:

  1. Edsger Dijkstra
    Edsger Dijkstra

    Edsger Wybe Dijkstra was a Netherlands computer science. He received the 1972 Turing Award for fundamental contributions in the area of programming languages, and was the Schlumberger Centennial Chair of Computer Sciences at University of Texas at Austin from 1984 until 2000....
    's structured programming, where the logic of a program is a structure composed of similar sub-structures in a limited number of ways.






    Discussion
    Ask a question about 'Structured programming'
    Start a new discussion about 'Structured programming'
    Answer questions from other users
    Full Discussion Forum



    Encyclopedia


    Structured programming can be seen as a subset or subdiscipline of procedural programming
    Procedural programming

    Procedural programming can sometimes be used as a synonym for imperative programming , but can also refer to a programming paradigm based upon the concept of the procedure call....
    , one of the major programming paradigm
    Programming paradigm

    A programming paradigm is a fundamental style of computer programming. . Paradigms differ in the concepts and abstractions used to represent the elements of a program and the steps that compose a computation ....
    s. It is most famous for removing or reducing reliance on the GOTO
    GOTO

    GOTO is a statement found in many computer programming languages. It is a combination of the English words wiktionary:go and wiktionary:to....
     statement
    Statement (programming)

    In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program is formed by a sequence of one or more statements....
    .

    Historically, several different structuring techniques or methodologies have been developed for writing structured programs. The most common are:

    1. Edsger Dijkstra
      Edsger Dijkstra

      Edsger Wybe Dijkstra was a Netherlands computer science. He received the 1972 Turing Award for fundamental contributions in the area of programming languages, and was the Schlumberger Centennial Chair of Computer Sciences at University of Texas at Austin from 1984 until 2000....
      's structured programming, where the logic of a program is a structure composed of similar sub-structures in a limited number of ways. This reduces understanding a program to understanding each structure on its own, and in relation to that containing it, a useful separation of concerns
      Separation of concerns

      In computer science, separation of concerns is the process of breaking a computer program into distinct features that overlap in functionality as little as possible....
      .
    2. A view derived from Dijkstra's which also advocates splitting programs into sub-sections with a single point of entry, but is strongly opposed to the concept of a single point of exit.
    3. Data Structured Programming or Jackson Structured Programming
      Jackson Structured Programming

      Jackson Structured Programming or JSP is a method for structured programming based on correspondences between data stream structure and program structure....
      , which is based on aligning data structure
      Data structure

      A data structure in computer science is a way of storing data in a computer so that it can be used efficiently. It is an organization of mathematical and logical concepts of data....
      s with program structures. This approach applied the fundamental structures proposed by Dijkstra, but as constructs that used the high-level structure of a program to be modeled on the underlying data structures being processed. There are at least 3 major approaches to data structured program design proposed by Jean-Dominique Warnier, Michael A. Jackson
      Michael A. Jackson

      Michael Anthony Jackson is a British computer scientist, and independent computing consultant in London, England. He is also part-time researcher at AT&T Research, Florham Park, NJ, United States, and visiting research professor at the Open University in the United Kingdom....
      , and Ken Orr.


    The two latter meanings for the term "structured programming" are more common, and that is what this article will discuss. Years after Dijkstra
    Edsger Dijkstra

    Edsger Wybe Dijkstra was a Netherlands computer science. He received the 1972 Turing Award for fundamental contributions in the area of programming languages, and was the Schlumberger Centennial Chair of Computer Sciences at University of Texas at Austin from 1984 until 2000....
     (1969), object-oriented programming
    Object-oriented programming

    Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
     (OOP) was developed to handle very large or complex programs (see below: Object-oriented comparison).

    Low-level structure

    At a low level, structured programs are often composed of simple, hierarchical program flow structures. These are sequence, selection, and repetition:

    • "Sequence" refers to an ordered execution of statements.


    • In "selection" one of a number of statements is executed depending on the state of the program. This is usually expressed with keywords such as if..then..else..endif
      Conditional statement

      In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified condition evaluates to true or false ....
      , switch
      Switch statement

      In computer programming, a switch statement is a type of control Statement that exists in most modern imperative programming languages . Its purpose is to allow the value of a variable or expression to control the flow of program execution....
      , or case
      Switch statement

      In computer programming, a switch statement is a type of control Statement that exists in most modern imperative programming languages . Its purpose is to allow the value of a variable or expression to control the flow of program execution....
      .


    • In "repetition" a statement is executed until the program reaches a certain state or operations are applied to every element of a collection. This is usually expressed with keywords such as while
      While loop

      In most computer programming languages, a while loop is a Control flow statement that allows code to be executed repeatedly based on a given Boolean datatype condition....
      , repeat
      Do while loop

      In most computer programming languages, a do while loop, sometimes just called a do loop, is a control flow statement that allows code to be executed repeatedly based on a given Boolean datatype condition....
      , for
      For loop

      In computer science a for loop is a programming language statement which allows code to be repeatedly execution . A for loop is classified as an iteration statement....
       or do..until
      Do while loop

      In most computer programming languages, a do while loop, sometimes just called a do loop, is a control flow statement that allows code to be executed repeatedly based on a given Boolean datatype condition....
      . Often it is recommended that each loop should only have one entry point (and in the original structural programming, also only one exit point), and a few languages enforce this.


    Some languages, such as Dijkstra's original Guarded Command Language
    Guarded Command Language

    The Guarded Command Language is a language defined by Edsger Dijkstra for predicate transformer semantics . The language is strictly theoretical, no compilers exist....
    , emphasise the unity of these structures with a syntax which completely encloses the structure, as in if..fi. In others, such as C
    C (programming language)

    C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
    , this is not the case, which increases the risk of misunderstanding and incorrect modification.

    A language is described as "block-structured" when it has a syntax for enclosing structures between bracketed keywords, such as an if-statement bracketed by if..fi as in ALGOL 68
    ALGOL 68

    ALGOL 68 is an imperative programming computer programming programming language that was conceived as a successor to the ALGOL 60 programming language, designed with the goal of a much wider scope of application and more rigorously defined syntax and semantics....
    , or a code section bracketed by BEGIN..END, as in PL/I
    PL/I

    PL/I is an imperative programming computer programming programming language designed for scientific, engineering, and business applications. It is one of the most feature-rich programming languages and one of the very first in the highly-feature-rich category....
    . However, a language is described as "comb-structured" when it has a syntax for enclosing structures within an ordered series of keywords. A "comb-structured" language has multiple structure keywords to define separate sections within a block, analogous to the multiple teeth or prongs in a comb separating sections of the comb. For example, in Ada
    Ada (programming language)

    Ada is a structured programming, statically typed, Imperative programming, and Object-oriented programming high-level language computer programming programming language, extended from Pascal and other languages....
    , a block is a 4-pronged comb with keywords DECLARE, BEGIN, EXCEPTION, END, and the if-statement in Ada is a 4-pronged comb with keywords IF, THEN, ELSE, END IF.

    Design

    Structured programming is often (but not always) associated with a "top-down" approach to design.

    Structured programming languages

    It is possible to do structured programming in any programming language, though it is preferable to use something like a procedural programming language
    Programming language

    A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
    . Since about 1970 when structured programming began to gain popularity as a technique, most new procedural programming languages have included features to encourage structured programming (and sometimes have left out features that would make unstructured programming easy). Some of the better known structured programming languages are ALGOL
    Algol

    Algol , known colloquially as the Demon Star, is a bright star in the constellation Perseus . It is one of the best known eclipsing binary, the first such star to be discovered, and also one of the first variable stars to be discovered....
    , Pascal
    Pascal (programming language)

    Pascal is an influential imperative programming and Procedural programming programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small and efficient language intended to encourage good programming practices using structured programming and data structure....
    , PL/I
    PL/I

    PL/I is an imperative programming computer programming programming language designed for scientific, engineering, and business applications. It is one of the most feature-rich programming languages and one of the very first in the highly-feature-rich category....
     and Ada
    Ada (programming language)

    Ada is a structured programming, statically typed, Imperative programming, and Object-oriented programming high-level language computer programming programming language, extended from Pascal and other languages....
    .

    History


    Theoretical foundation

    The structured program theorem
    Structured program theorem

    The structured program theorem is a result in programming language theory. It states that every computable function can be implemented in a programming language that combines subprograms in only three specific ways....
     provides the theoretical basis of structured programming. It states that three ways of combining programs—sequencing, selection, and iteration—are sufficient to express any computable function
    Computable function

    Computable functions are the basic objects of study in recursion theory. The set of computable functions is equivalent to the set of Turing-computable functions and partial recursive functions....
    . This observation did not originate with the structured programming movement; these structures are sufficient to describe the instruction cycle
    Instruction cycle

    An instruction cycle is the time period during which a computer processes a machine language instruction from its computer storage or the sequence of actions that the central processing unit performs to execute each machine code instruction in a program....
     of a central processing unit
    Central processing unit

    A central processing unit is an electronic circuit that can execute computer programs. This broad definition can easily be applied to many early computers that existed long before the term "CPU" ever came into widespread usage....
    , as well as the operation of a Turing machine
    Turing machine

    Turing machines are basic abstract symbol-manipulating devices which, despite their simplicity, can be adapted to simulate the logic of any computer algorithm....
    . Therefore a processor is always executing a "structured program" in this sense, even if the instructions it reads from memory are not part of a structured program. However, authors usually credit the result to a 1966 paper by Böhm and Jacopini, possibly because Dijkstra
    Dijkstra

    Dijkstra is a Dutch language family name that may refer to:*Edsger W. Dijkstra , computing scientist*Rineke Dijkstra , photographer*Sjoukje Dijkstra , retired figure skater...
     cited this paper himself. The structured program theorem does not address how to write and analyze a usefully structured program. These issues were addressed during the late 1960s and early 1970s, with major contributions by Dijkstra
    Dijkstra

    Dijkstra is a Dutch language family name that may refer to:*Edsger W. Dijkstra , computing scientist*Rineke Dijkstra , photographer*Sjoukje Dijkstra , retired figure skater...
    , Robert W. Floyd, Tony Hoare, and David Gries
    David Gries

    David Gries is a computer scientist at Cornell University. He is currently Associate Dean for Undergraduate Programs in the Cornell University College of Engineering....
    .

    Debate

    P. J. Plauger
    P. J. Plauger

    P. J. Plauger is an author and entrepreneur.He has written and co-written articles and books about programming style, software tools, and the C ....
    , an early adopter of structured programming, described his reaction to the structured program theorem:
    Us converts waved this interesting bit of news under the noses of the unreconstructed assembly-language programmers who kept trotting forth twisty bits of logic and saying, 'I betcha can't structure this.' Neither the proof by Böhm and Jacopini nor our repeated successes at writing structured code brought them around one day sooner than they were ready to convince themselves.


    In 1967 a letter from Dijkstra appeared in Communications of the ACM
    Communications of the ACM

    Communications of the ACM is the flagship monthly journal of the Association for Computing Machinery . First published in 1957, CACM is sent to all ACM members, currently numbering about 80,000....
     with the heading "Goto statement considered harmful
    Considered harmful

    In computer science and related disciplines, considered harmful is a phrase popularly used in the titles of diatribes and other critical essays ....
    ." The letter, which cited the Böhm and Jacopini proof, called for the abolishment of unconstrained GOTO
    GOTO

    GOTO is a statement found in many computer programming languages. It is a combination of the English words wiktionary:go and wiktionary:to....
     from high-level languages in the interest of improving code quality. This letter is usually cited as the beginning of the structured programming debate.

    Although, as Plauger mentioned, many programmers unfamiliar with the theorem doubted its claims, the more significant dispute in the ensuing years was whether structured programming could actually improve software's clarity, quality, and development time enough to justify training programmers in it. Dijkstra claimed that limiting the number of structures would help to focus a programmer's thinking, and would simplify the task of ensuring the program's correctness by dividing analysis into manageable steps. In his 1969 Notes on Structured Programming, Dijkstra wrote:

    When we now take the position that it is not only the programmer's task to produce a correct program but also to demonstrate its correctness in a convincing manner, then the above remarks have a profound influence on the programmer's activity: the object he has to produce must be usefully structured.


    …In what follows it will become apparent that program correctness is not my only concern, program adaptability or manageability will be another… 1


    Donald Knuth
    Donald Knuth

    Donald Ervin Knuth is a renowned computer science and Emeritus of the Art of Computer Programming at Stanford University.Author of the seminal multi-volume work The Art of Computer Programming , Knuth has been called the "father" of the run-time analysis, contributing to the development of, and systematizing formal mathematical techn...
     accepted the principle that programs must be written with provability in mind, but he disagreed (and still disagrees) with abolishing the GOTO statement. In his 1974 paper, "Structured Programming with Goto Statements", he gave examples where he believed that a direct jump leads to clearer and more efficient code without sacrificing provability. Knuth proposed a looser structural constraint: It should be possible to draw a program's flow chart with all forward branches on the left, all backward branches on the right, and no branches crossing each other. Many of those knowledgeable in compiler
    Compiler

    A compiler is a computer program that transforms source code written in a programming language into another computer language . The most common reason for wanting to transform source code is to create an executable program....
    s and graph theory
    Graph theory

    In mathematics and computer science, graph theory is the study of graph : mathematical structures used to model pairwise relations between objects from a certain collection....
     have advocated allowing only reducible flow graphs.

    Structured programming theorists gained a major ally in the 1970s after IBM
    IBM

    International Business Machines Corporation, abbreviated IBM and nicknamed "Big Blue" , is a multinational corporation computer technology and consulting corporation headquartered in Armonk, New York, New York, United States....
     researcher Harlan Mills
    Harlan Mills

    Harlan D. Mills was Professor of Computer Science at the Florida Institute of Technology and founder of Software Engineering Technology, Inc. of Vero Beach, Florida ....
     applied his interpretation of structured programming theory to the development of an indexing system for the New York Times research file. The project was a great engineering success, and managers at other companies cited it in support of adopting structured programming, although Dijkstra criticized the ways that Mills's interpretation differed from the published work.

    As late as 1987 it was still possible to raise the question of structured programming in a computer science journal. Frank Rubin did so in that year with a letter, "'GOTO considered harmful' considered harmful." Numerous objections followed, including a response from Dijkstra that sharply criticized both Rubin and the concessions other writers made when responding to him.

    Outcome

    By the end of the 20th century nearly all computer scientists were convinced that it is useful to learn and apply the concepts of structured programming. High-level programming languages that originally lacked programming structures, such as FORTRAN
    Fortran

    Fortran is a general-purpose programming language, procedural programming language, imperative programming language programming language that is especially suited to numerical analysis and scientific computing....
    , COBOL
    COBOL

    COBOL is one of the oldest programming languages still in active use. Its name is an acronym for COmmon Business-Oriented Language, defining its primary domain in business, finance, and administrative systems for companies and governments....
    , and BASIC
    BASIC

    In computer programming, BASIC is a family of high-level programming languages. The Dartmouth BASIC was designed in 1964 by John George Kemeny and Thomas Eugene Kurtz at Dartmouth College in New Hampshire, United States to provide computer access to non-science students....
    , now have them.

    Common deviations


    Exception handling

    Although there is almost never a reason to have multiple points of entry to a subprogram, multiple exits are often used to reflect that a subprogram may have no more work to do, or may have encountered circumstances that prevent it from continuing.

    A typical example of a simple procedure would be reading data from a file and processing it: open file; while (reading not finished) process read data; finish the subprogram;

    The "stop and inform" may be achieved by throwing an exception, second return from the procedure, labelled loop break, or even a goto. As the procedure has 2 exit points, it breaks the rules of Dijkstra's structured programming. Coding it in accordance with single point of exit rule would be very cumbersome. If there were more possible error conditions, with different cleanup rules, single exit point procedure would be extremely hard to read and understand, very likely even more so than an unstructured one with control handled by goto statements. On the other hand, structural programming without such a rule would result in very clean and readable code.

    Most languages have adopted the multiple points of exit form of structural programming. C
    C (programming language)

    C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
     allows multiple paths to a structure's exit (such as "continue", "break", and "return"), newer languages have also "labelled breaks" (similar to the former, but allowing breaking out of more than just the innermost loop) and exceptions.

    State machines

    Some programs, particularly parsers and communications protocol
    Communications protocol

    In the field of telecommunications, a communications protocol is the set of standard rules for data representation, Signalling , authentication and Error detection and correction required to send information over a communications channel....
    s, have a number of states
    State (computer science)

    In computer science and automata theory, a state is a unique configuration of information in a program or machine. It is a concept that occasionally extends into some forms of systems programming such as Lexical analysiss and parsers....
     that follow each other in a way that is not easily reduced to the basic structures. It is possible to structure these systems by making each state-change a separate subprogram and using a variable to indicate the active state (see trampoline
    Trampoline (computers)

    In computer programming, the word "trampoline" has a number of meanings, associated with jumps.* Trampolines are memory locations holding addresses pointing to interrupt service routines, I/O routines, etc....
    ). However, some programmers (including Knuth) prefer to implement the state-changes with a jump to the new state.

    Object-oriented comparison

    In the 1960s, language design was often based on textbook examples of programs, which were generally small (due to the size of a textbook); however, when programs became very large, the focus changed. In small programs, the most common statement is generally the assignment statement; however, in large programs (over 10,000 lines), the most common statement is typically the procedure-call to a subprogram. Ensuring parameters are correctly passed to the correct subprogram becomes a major issue.

    Many small programs can be handled by coding a hierarchy of structures; however, in large programs, the organization is more a network of structures, and insistence on hierarchical structuring for data and procedures can produce cumbersome code with large amounts of "tramp data." For example, a text-display program that allows dynamically changing the font-size of the entire screen would be very cumbersome if coded by passing font-size data through a hierarchy. Instead, a subsystem could be used to control the font data through a set of accessor functions that set or retrieve data from a common area controlled by that font-data subsystem. Databases are a common way around tramping.

    The FORTRAN
    Fortran

    Fortran is a general-purpose programming language, procedural programming language, imperative programming language programming language that is especially suited to numerical analysis and scientific computing....
     language has used labelled COMMON-blocks to separate global program data into subsystems (no longer global) to allow program-wide, network-style access to data, such as font-size, but only by specifying the particular COMMON-block name. Confusion could occur in FORTRAN by coding alias names and changing data-types when referencing the same labelled COMMON-block yet mapping alternate variables to overlay the same area of memory. Regardless, the labelled-COMMON concept was very valuable in organizing massive software systems and lead to the use of object-oriented programming to define subsystems of centralized data controlled by accessor functions. Changing data into other data-types was performed by explicitly converting, or casting, data from the original variables.

    Global subprogram names were recognized as just as dangerous (or even more dangerous) than global variables or blank COMMON, and subsystems were limited to isolated groups of subprogram names, such as naming with unique prefixes or using Java
    Java (programming language)

    Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
     package names.

    Although structuring a program into a hierarchy might help to clarify some types of software, even for some special types of large programs, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy. The object-oriented approach is allegedly more flexible, by separating a program into a network of subsystems, with each controlling their own data, algorithms, or devices across the entire program, but only accessible by first specifying named access to the subsystem object-class, not just by accidentally coding a similar global variable name. Rather than relying on a structured-programming hierarchy chart, object-oriented programming needs a call-reference index to trace which subsystems or classes are accessed from other locations.

    Modern structured systems have tended away from deep hierarchies found in the 1970s and tend toward "event driven" architectures, where various procedural events are designed as relatively independent tasks.

    Structured programming, as a forerunner to object-oriented programming
    Object-oriented programming

    Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
    , noted some crucial issues, such as emphasizing the need for a single exit-point in some types of applications, as in a long-running program with a procedure that allocates memory and should deallocate that memory before exiting and returning to the calling procedure. Memory leak
    Memory leak

    In computer science, a memory leak is a particular type of unintentional memory consumption by a computer program where the program fails to release dynamic memory when no longer needed....
    s that cause a program to consume vast amounts of memory could be traced to a failure to observe a single exit-point in a subprogram needing memory deallocation.

    Similarly, structured programming, in warning of the rampant use of goto-statements, led to a recognition of top-down discipline in branching, typified by Ada
    Ada (programming language)

    Ada is a structured programming, statically typed, Imperative programming, and Object-oriented programming high-level language computer programming programming language, extended from Pascal and other languages....
    's GOTO that cannot branch to a statement-label inside another code block. However, "GOTO WrapUp" became a balanced approach to handling a severe anomaly without losing control of the major exit-point to ensure wrap-up (for deallocating memory, deleting temporary files, and such), when a severe issue interrupts complex, multi-level processing and wrap-up code must be performed before exiting.

    The various concepts behind structured programming can help to understand the many facets of object-oriented programming
    Object-oriented programming

    Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
    .

    See also

    • Control flow
      Control flow

      In computer science control flow refers to the order in which the individual statement , Instruction or function calls of an imperative programming or functional programming computer program are execution or evaluated....
       (more detail of high-level control structures)
    • Minimal evaluation
      Minimal evaluation

      Short-circuit evaluation or minimal evaluation denotes the semantics of some boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression: when the first argument of and evaluates to false
    • Programming paradigm
      Programming paradigm

      A programming paradigm is a fundamental style of computer programming. . Paradigms differ in the concepts and abstractions used to represent the elements of a program and the steps that compose a computation ....
      s
    • Structured exception handling
    • Structure chart
      Structure chart

      File:Structured Chart Example.jpgA Structure Chart in software engineering and organizational theory is a chart, that shows the breakdown of the configuration system to the lowest manageable levels....


    External links