Definition

structured programming (modular programming)

What is structured programming (modular programming)?

Structured programming, or modular programming, is a programming paradigm that facilitates the creation of programs with readable code and reusable components. All modern programming languages support structured programming, but the mechanisms of support -- like the syntax of the programming languages -- vary.

When modules or elements of code can be reused from a library, it may also be possible to build structured code using modules written in different languages, as long as they can obey a common module interface or application program interface specification. However, when modules are reused, it's possible to compromise data security and governance, so it's important to define and enforce a privacy policy controlling the use of modules that bring with them implicit data access rights.

Structured programming encourages dividing an application program into a hierarchy of modules or autonomous elements, which, in turn, may contain other such elements. Within each element, code may be further structured using blocks of related logic designed to improve readability and maintainability. These may include case, which tests a variable against a set of values, and repeat, while and for, which construct loops that continue until a condition is met. In all structured programming languages, an unconditional transfer of control, or goto statement, is deprecated and sometimes not even available.

Difference between structured and unstructured programming languages

A structured programming language facilitates or enforces structured programming practices. Unstructured languages can also support these practices, but that requires specific steps in program design and implementation. Structured programming practices thus date to the emergence of structured programming languages.

The theoretical basis for structured programming goes back to the 1950s, with the emergence of Algorithmic Language (ALGOL) 58 and 60. Up to then, code clarity was reduced by the need to build condition/action tests by having programmers write linked tests and actions explicitly -- using the goto statement or its equivalent -- resulting in what was often called spaghetti code. ALGOL included block structure, where an element of code included a condition and an action.

Modular programming, which is today seen as synonymous with structured programming, emerged a decade later as it became clear that reuse of common code could improve developer productivity. In modular programming, a program is divided into semi-independent modules, each of which are called when needed. Purists argue that modular programming requires actual independence of modules, but most development teams consider any program that divides logic into separate elements, even if those elements exist within the same program, as modular.

Modern programming languages are universally capable of producing structured code. Similarly, they're also capable of producing code fairly described as unstructured if used incorrectly. Some would say that an unstructured programming language contains goto statements and, therefore, does not require a call to a separate module, which then returns when complete, but that definition is unnecessarily restrictive. It's better to say that the mechanisms for enforcing structure vary by language, with some languages demanding structure and other accepting less-structured code.

Types of structured programming

There are three categories of structured programming:

  1. Procedural programming. Defines modules as procedures or functions that are called with a set of parameters to perform a task. A procedural language begins a process, which is then given data. It is also the most common category and is subdivided into the following:
    1. Service-oriented programming simply defines reusable modules as services with advertised interfaces.
    2. Microservice programming focuses on creating modules that do not store data internally and so are scalable and resilient in cloud deployment.
    3. Functional programming, technically, means that modules are written from functions, and that these functions' outputs are derived only from their inputs. Designed for serverless computing, the definition of functional programming has since expanded to be largely synonymous with microservices.
  2. Object-oriented programming (OOP). Defines a program as a set of objects or resources to which commands are sent. An object-oriented language defines a data resource and sends it to process commands. For example, the procedural programmer might say, "Print(object)," while the OOP programmer might say, "Tell Object to Print."
  3. Model-based programming. The most common example of this is database query languages. In database programming, units of code are associated with steps in database access and update or run when those steps occur. The database and database access structure determine the structure of the code. Another example of a model-based structure is Reverse Polish notation, a math-problem structure that lends itself to efficient solving of complex expressions. Quantum computing is another example of model-based structured programming; the quantum computer demands a specific model to organize steps, and the language simply provides it.

Components of structured programming

Structured programs consist of a structural hierarchy starting with the main process and decomposing downward to lower levels as the logic dictates. These lower structures are the modules of the program, and modules may contain both calls to other lower-level modules and blocks representing structured condition/action combinations. All of this can be combined into a single module or unit of code, or broken down into multiple modules, resident in libraries.

Modules can be classified as procedures or functions. A procedure is a unit of code that performs a specific task, usually referencing a common data structure available to the program at large. Much of the data operated on by procedures is external. A function is a unit of code that operates on specific inputs and returns a result when called.

Structured programs and modules typically have a header file or section that describes the modules or libraries referenced and the structure of the parameters and module interface. In some programming languages, the interface description is abstracted into a separate file, which is then implemented by one or more other units of code.

Advantages of structured programming

There are multiple advantages to structured programming. For one, it encourages top-down implementation, which improves both readability and maintainability of code. Structured programming also promotes code reuse, since even internal modules can be extracted and made independent, residents in libraries, described in directories and referenced by many other applications. Lastly, it's widely agreed that structured programming improves development time and code quality.

These advantages are normally seen as compelling -- even decisive -- and nearly all modern software development employs structured programming.

Disadvantages of structured programming

The biggest disadvantage of structured programming is a reduction in execution efficiency, followed by greater memory usage. Both these problems arise from the introduction of calls to a module or process, which then returns to the caller when it's done. System parameters and system resources are saved on a stack -- a queue organized as LIFO, or last in, first out -- and popped when needed. The more program logic is decomposed, meaning the more modules are involved, the greater the overhead associated with the module interface. All structured programming languages are at risk to overstructuring and loss of efficiency.

Structured programming can also be applied incorrectly if the type of structure selected isn't right for the task at hand. The best-known example is the solving of math problems. RPL is an efficient way to state and solve a math problem because it eliminates the need to explicitly state execution order and eliminates recursion in code. However, if that problem were to be posed in structured programming procedural or object form, the resulting code would be much less efficient than the RPL version.

This was last updated in March 2023

Continue Reading About structured programming (modular programming)

Dig Deeper on Software design and development