Simplify code

Asked

Viewed 268 times

3

I did a program, based on a study I read. The study says it takes 10,000 hours to become genius/master in any area.

Can you simplify what I did? In the menu I could only do so, if you give me suggestions I thank you. I have a great desire to learn and ideas for complexing the program are also welcome.

Here I leave the code:

#include <stdio.h>
float converterDiasEmAnos(float dias) {
    return dias/365;
}
float converterHorasEmDias(int horas) {
    return 10000/horas;
}
int obterMeses(float anos) {
    return (anos -(int)anos)*(12);
}

int main()
{

    //prototipos
    float converterDiasEmAnos(float dias);
    float converterHorasEmDias(int horas);
    int obterMeses(float anos);
    printf("\t\tPrograma para saber quanto demora a tornar-se génio!\n\n");
    //variaveis locais
    int num_horas,meses,escolha;
    float anos,dias;
    //MENU
    printf("\nDigite a opcao correta:\n1-Continuar com o programa\n0-Para sair!\n\n");
    scanf("%d",&escolha);
    switch(escolha) {
        case 1:
            //pergunta
            printf("Quantas horas vai dedicar por dia para ser génio?");
            scanf("%d",&num_horas);
            //chamada
            dias=converterHorasEmDias(num_horas);
            anos=converterDiasEmAnos(dias);
            meses=obterMeses(anos);
            //afirmacao
            printf("Voce vai demorar %.0f dias ou seja aproximadamente %.0f anos\n%d meses para ficar génio\n",dias,anos,meses);
            break;
        case 0:
            return 0;
            break;
        default:
            printf("Digite corretamente!");
            break;

    }
return 0;
}
  • Is there an error in the code?

  • 3

    Hello, @Miguel. I edited your question to get the code indented a little bit, and take out the HTML formatting you used. To learn how to format a post, see this link: Help from the Markdown. To see the changes made, just look at the Historic edits. If you want to [Edit] your question and add more details to let us know where you need help, this link has some tips: "[Ask]".

  • 1

    Suggestion: always use it double for floating comma values. There is no advantage in using float.

  • 1

    Suggestion: usually prototypes put themselves out of any function, just after includes. By placing prototypes within a function they only apply to that function; if you have other functions in your program, you have to repeat (unnecessarily) the prototypes.

1 answer

9


In his previous question I had already given a good organized in the code, now it’s worse again. There’s not much to simplify, it can only organize better.

One of the mistakes you make is not to maintain consistency. Time uses one way and time uses another. Time organizes, time piles up everything. Hora uses unnecessary things. Pay attention character by character to what I wrote to see how it looks better.

What I "simplified":

I removed the prototypes, they are only needed in three situations: a) will use in another file, b) there are circular references between the functions or c) the code is too disorganized. As there are none of these cases in this code, I removed them (I had already done before in the previous question).

I have removed the separate variable statements, this is old style and unnecessary. The statements should be as close as possible to their use. It has to narrow the scope as far as possible.

I took the comments because they’re not being useful for anything. Comments should explain why you did something that seems strange and not say what the code does. There are myths about the use of comments. If you think an excerpt of code deserves a comment, he’s either confused or he deserves to be in a separate role. I preferred not to separate the menu, but it’s a possibility.

One of the reasons I haven’t touched the menu is that I think the code is half over. That menu doesn’t seem to make any sense. If it’s really to simplify, the way it is, I’d just take it off. I didn’t take it off because I think you’re gonna do something useful with it later. If you don’t, just take it out. Don’t put anything in the code that doesn’t add something really useful to the user experience. At the moment it only causes inconvenience.

I took a variable statement. On the other question I had said that it was unnecessary, but I had left it because it might be good for something if I messed with the code. I did it now to simplify.

I took a break after the return, it would never run. It was actually to take out other redundant things, but then the switch all would have to be taken out because it has no function in the code. And finally the menu message would also have to be taken out.

It’s obviously hard to make a code review in code that is half full. Next time you ask for this, present a complete code, even if you only have to present a snippet.

#include <stdio.h>
float converterDiasEmAnos(float dias) {
    return dias / 365;
}
float converterHorasEmDias(int horas) {
    return 10000 / horas;
}
int obterMeses(float anos) {
    return (anos - (int)anos) * 12;
}
int main() {
    printf("\t\tPrograma para saber quanto demora a tornar-se génio!\n\n");
    int escolha;
    printf("\nDigite a opcao correta:\n1-Continuar com o programa\n0-Para sair!\n\n");
    scanf("%d", &escolha);
    switch(escolha) {
        case 1:
            int num_horas;
            printf("Quantas horas vai dedicar por dia para ser genio?");
            scanf("%d", &num_horas);
            float dias = converterHorasEmDias(num_horas);
            float anos = converterDiasEmAnos(dias);
            printf("Voce vai demorar %.0f dias ou seja aproximadamente %.0f anos\n%d meses para ficar genio\n", dias, anos, obterMeses(anos));
            break;
        case 0:
            return 0;
        default:
            printf("Digite corretamente!");
            break;
    }
}

I’m going to put the most simplified code here, apart from the useless parts. You may not like it, but I’m doing what the question asks, taking away everything that’s useless simplifies the code:

#include <stdio.h>
float converterDiasEmAnos(float dias) {
    return dias / 365;
}
float converterHorasEmDias(int horas) {
    return 10000 / horas;
}
int obterMeses(float anos) {
    return (anos - (int)anos) * 12;
}
int main() {
    printf("\t\tPrograma para saber quanto demora a tornar-se génio!\n\n");
    int num_horas;
    printf("Quantas horas vai dedicar por dia para ser genio?");
    scanf("%d", &num_horas);
    float dias = converterHorasEmDias(num_horas);
    float anos = converterDiasEmAnos(dias);
    printf("Voce vai demorar %.0f dias ou seja aproximadamente %.0f anos\n%d meses para ficar genio\n", dias, anos, obterMeses(anos));
}

One last simplification since the functions are being used only once. I know you may be wanting to use for other things, but in this case it is not necessary to have functions and simplify is usually to eliminate everything that does not need to be used:

#include <stdio.h>
int main() {
    printf("\t\tPrograma para saber quanto demora a tornar-se génio!\n\n");
    int num_horas;
    printf("Quantas horas vai dedicar por dia para ser genio?");
    scanf("%d", &num_horas);
    float dias = num_horas / 365;
    float anos = 10000 / dias;
    printf("Voce vai demorar %.0f dias ou seja aproximadamente %.0f anos\n%d meses para ficar genio\n", dias, anos, (anos - (int)anos) * 12);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Simplification can make it more legible, but it can make it worse too. I could make others that would certainly make it worse. Some that I did may have worsened depending on the context. Looking only at this code did not get worse. It’s quite understandable, but it might not be in a larger context.

  • Please avoid long discussions in the comments; your talk was moved to the chat

  • I think the menu was supposed to be on a loop (while(true) {..., hence the option to quit the program and the functions. It is quite common this type of menu in examples of programs for beginners =)

Browser other questions tagged

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