What are the characteristics of structured programming?

8

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

Here follows a program made in C which 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:

  

You can cite examples in the C language.

    
asked by anonymous 09.04.2016 / 04:38

1 answer

8

C is a language that allows and even facilitates structured programming , which in theory can be applied in any language of high-level programming. In fact today there is virtually no language that encourages unstructured programming.

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

There are some "versions" of structured programming. One of the most accepted is that you can not use goto indiscriminately and give preference to more organized flow control structures such as while or functions, for example. She preaches to avoid macarronic code . The use of functions to aid in flow is encouraged. But nothing is said other than flow.

The term was important in the 1960s and 1970s when it was common to program in a poorly structured way, favoring optimization rather than readability (remembering that computers were far less powerful before, and compilers were well streamlined, with no 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 an evolution of structured programming, or at least a more specific definition. Procedural only encourages code modularisation, 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 stream. If 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 structuring capabilities, and other organizations.

Analyzing the example

Interestingly the code in the example even shows some structure, but it's 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 the single responsibility in all three functions. This principle is the basis of the procedural paradigm and even more so in the modular . 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
    return 0;
}

I do not know if the code works, I just wanted to get the idea across.

    
09.04.2016 / 06:21