What are the characteristics of structured programming?

Asked

Viewed 2,664 times

12

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 look the same to me, and it doesn’t change the way you program.

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:

Examples can be cited in C.

1 answer

10


C is a language that allows and even facilitates the 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 does not encourage or practically does not allow.

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 cannot use goto indiscriminately and give preference to more organized flow control structures such as while or functions, for example. It preaches avoiding noodle code. The use of functions to help flow is encouraged, but nothing is said but flow.

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

Procedural

Indeed procedural programming is an evolution of structured programming, or at least a more specific definition. The procedural only encourages more modularization of the 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, i.e., command by command, changing the state of the data, including the execution flow. Whether the flow is controlled in a structured way is a more evolved paradigm. The vast majority of programming languages mainstream are predominantly imperative with excellent structuring capabilities, and other organizations.

Analyzing the example

Interestingly the example code 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 the sole responsibility, in all three functions. This principle is the basis of the procedural paradigm and even more 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 in the Github for future reference.

I don’t know if the code works.

  • 1

    Correct code http://ideone.com/JiLNhV

Browser other questions tagged

You are not signed in. Login or sign up in order to post.