What are the features of structured programming?

I would like to know what are the characteristics that define the paradigm of structured programming, and what is the difference of this paradigm with the paradigm of procedural programming? Both to me seems the same thing, and does not change the way of programming.

Here follows a program made in C that is a structured language for illustration:

#include <stdio.h>

void soma();
void subtrai();

int valor1, valor2;

int main(void)
{
    printf("Valor 1: ");
    scanf("%d", &valor1);

    printf("Valor 2: ");
    scanf("%d", &valor2);

    soma();
    subtrai();

    return 0;
}

void soma()
{
    printf("\n\nSoma: %d", valor1 + valor2);
}

void subtrai()
{
    printf("\n\nSubtracao: %d", valor1 - valor2);
}

Note:

Can cite examples in the C language.

Author: Maniero, 2016-04-09

1 answers

C is a language that allows and even facilitates structured programming, which in theory can be applied in any high-level programming language. In fact today there is practically no more language that encourages Unstructured programming, even some that encouraged today have stronger dialects that do not encourage or practically do not allow.

Or paradigm is often applied in code. Most programming languages allow you to use any paradigm. Of course, they facilitate some more than others. Of course, similar paradigms are more easily adapted.

There are some "versions" of what is structured programming. One of the most accepted is that one cannot use goto indiscriminately and give preference to more organized flow control structures like while or functions, for example. She preaches to avoid macaroni code . The use of functions to help flow is encouraged, but nothing it speaks beyond the flow.

The term was important in the 60s and 70s when it was common to program in a poorly structured way, favoring optimization and not readability (remembering that before computers were much less powerful and compilers were well simplified, not having optimizations. And the codes were very simple. Today it is more common to use the terms imperative and procedural.

Procedural

In fact procedural programming is a evolution of structured programming, or at least a more specific definition. Procedural only encourages more modularization of code, but it is not yet modular programming .

Imperative

The imperative paradigm (comparison with functional and declarative) defines that the code will be executed step by step, that IS, command by command, changing the state of the data, including the execution flow. If the flow is controlled in a structured way it is a more evolved paradigm. The vast majority of mainstream programming languages are predominantly imperative with excellent capabilities for structuring, and other organizations.

Parsing the example

Interestingly the code in the example even shows some structure, but it is not a code with good structure. I know it's just an example, but it violates a principle that is best explored today, which is that of Single Responsibility , in all three functions. This principle is the basis of the procedural paradigm and even more so in the modular. A better written code would be more procedural and modular:

void imprimeSoma(int valor1, int valor2) { //preferi mudar o nome do que mudar a função
    printf("\n\nSoma: %d", valor1 + valor2);
}
void imprimeSubtracao(int valor1, int valor2) { //se mantiver subtrai, não poderia imprimir
    printf("\n\nSubtracao: %d", valor1 - valor2);
}
int pegaValor(char * texto) {
    printf(texto);
    int valor;
    scanf("%d", &valor);
    return valor;
}
int main(void) {
    int valor1 = pegaValor("Valor 1: "); //é mais procedural deixar a variável local
    int valor2 = pegaValor("Valor 2: ");
    imprimeSoma(valor1, valor2); //os dados devem ser locais e passados para funções
    imprimeSubtracao(valor1, valor2); //a função comunica com parâmetros
}

I put on GitHub for future reference.

I don't know if the code works, I just wanted to pass on the idea.

 9
Author: Maniero, 2020-06-11 14:45:34