Structured programming (sometimes known as 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, varies.Content Continues Below
Structured programming encourages dividing an application program into a hierarchy of modules or autonomous elements, which may, in turn, 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; 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. These practices can also be supported with unstructured languages, but that will require 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 the ALGOL 58 and 60 languages. 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, thus, 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
Structured programming can be divided into three categories, including:
Procedural programming. Defines modules as "procedures" or "functions" that are called with a set of parameters to perform a task. A procedural language will begin a process, which is then given data. It is also the most common category and has recently been subdivided into the following:
- Service-oriented programming simply defines reusable modules as "services" with advertised interfaces.
- Microservice programming focuses on creating modules that do not store data internally, and so are scalable and resilient in cloud deployment.
- 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.
Object-oriented programming (OOP). Defines a program as a set of objects or resources to which commands are sent. An object-oriented language will define a data resource and send it to process commands. For example, the procedural programmer might say "Print(object)" while the OOP programmer might say "Tell Object to Print".
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 will determine the structure of the code. Another example of a model-based structure is Reverse Polish Notation (RPN), a math-problem structure that lends itself to efficient solving of complex expressions. Quantum computing, just now emerging, 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
At the high level, 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
The primary advantages of structured programming are:
- It encourages top-down implementation, which improves both readability and maintainability of code.
- It 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.
- It's widely agreed that development time and code quality are improved through structured programming.
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 "over-structuring" 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.