jetbrains rider, is there a way to make my code smaller?

Answered

see my program?

#include <iostream>
using namespace std;

int main()
{
    double NumberPi = 3.14159;
    double NumberE = 2.718281828;

    int count = 0;

    bool Condition = true;

    do
    {
        cout << "The number Pi is: " << NumberPi << endl;
        cout << "The number E is: " << NumberE << endl;
        cout << "The number count is: " << count << endl;
        cout << "Pi + E * count = " << NumberPi + NumberE * count << endl;

        count++;
        if (count <= 100)
        {
            Condition = true;
        }
        else
        {
            Condition = false;
        }
    }while(Condition);

    system("pause");
}

 

I want to know if there is a plugin or way to make the code smarter/cleaner. 

 

this is what I want it to do:

 

#include <iostream>
using namespace std;

int main()
{
    double NumberPi = 3.14159;
    double NumberE = 2.718281828;

    int count = 0;

    bool Condition = true;

    do
    {
        cout << "The number Pi is: " << NumberPi << endl;
        cout << "The number E is: " << NumberE << endl;
        cout << "The number count is: " << count << endl;
        cout << "Pi + E * count = " << NumberPi + NumberE * count << endl;

        count++;
        if (count > 100)
        {
            Condition = false;
        }
    }while(Condition);

    system("pause");
}
0
3 comments

Please try with the refactoring rules for the if statements.

0

i select the code :

 

 

if (count <= 100)
{
    Condition = true;
}
else
{
    Condition = false;
}

 

and rick click the code and select “Refactor this”, and it says “refactor this: extract method”. then it generates this code:

 

#include <iostream>
using namespace std;

void do_work(int count, bool& Condition)
{
    if (count <= 100)
    {
        Condition = true;
    }
    else
    {
        Condition = false;
    }
}

int main()
{
    double NumberPi = 3.14159;
    double NumberE = 2.718281828;

    int count = 0;

    bool Condition = true;

    do
    {
        cout << "The number Pi is: " << NumberPi << endl;
        cout << "The number E is: " << NumberE << endl;
        cout << "The number count is: " << count << endl;
        cout << "Pi + E * count = " << NumberPi + NumberE * count << endl;

        count++;
        do_work(count, Condition);
    }while(Condition);

    system("pause");
}
0

Structure-based refactoring/inspection couldn't solve operation flow issues. Utilizing AI Assistant for suggesting refactoring can offer an improved solution. In AI Actions | Suggest Refactoring, it provides a better way to handle the operation flow :

For comprehensive C++ development, CLion offers a better experience. Rider provides C++ support for Unreal Engine projects only.

0

Please sign in to leave a comment.