4
\$\begingroup\$

i'm a beginner in C++
i decided to learn the Scary C++ Programing Language
i am a slow learner and after 2 Months learning C++ i saw the need to have a Nice and Tidy progress bar for my programs.

Here what i wrote:

#include <iostream>
#include <string>
#include <iomanip>
#include <unistd.h>

// That function controls the inner Bar

void printProgressBar(char  fill = '.',
                      char  highlight = '*',
                      int   barSize = 10,
                      float startingProgress = 0.0f
                     )
{
        for (int pos = 0; pos < (barSize-(startingProgress*10)); ++pos)
        {
        std::cout << "\r[" << std::string(pos, fill) << highlight << std::flush;
        usleep(50000);
        }
}

// That function controls the outer Bar

void progressBarBox(char highlight = '*', float endPoint = 1.0f){
        std::cout << std::string(endPoint*10, highlight);
        std::cout << ']' << std::flush;
}

int main(int argc, char* argv[]) {
        const int  barSize{3};
        const char barFill{'.'};
        const char barHighlight{'*'};

        progressBarBox(barFill, barSize+0.1f);

        float progress {0.0f};
        while (progress < barSize)
        {
                  printProgressBar(barFill, barHighlight, barSize*10, progress);

        progressBarBox(barHighlight, progress);
        progress += 0.1;
        }

        std::cout << "\nDone" << std::endl;

        return 0;
}

The problem is: with python i never cared about best practices, clean of code or code readbility

I Have many questions about my solution

My Questions:

  1. How would you rate my code, what to improve ?
  2. how to improve his readbility?
  3. this code would be fine to work with or a headache to mantain, how to improve?
  4. is there Better ways ( that are not so complex too ) to achieve the same ? with the star moving and everything.
  5. Is there a best practice i'm not following or missing ?
  6. i hardcoded some values is that bad ?
  7. a friend of mine said that this code will have a bad performance because of my use of string, is that true ?

I know thats a lot of questions and i am grateful for any contribution or help.

 Got it ?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Here are some things that may help you improve your program.

Use consistent formatting

The code as posted has inconsistent indentation which makes it a bit harder to read and understand. Pick a style and apply it consistently.

Think carefully about requirements

You wrote:

a friend of mine said that this code will have a bad performance because of my use of string, is that true ?

Maybe, but it's important to think about the requirements first before chasing phantoms of imagined performance gains. The first question is, "does it matter how fast it is?" I'd say that any code that contains usleeep(50000); within a loop probably isn't performance-critical and so it doesn't much matter.

So then that leads to the question of what does matter which directly leads to the topic of requirements. What does the code need to do? Is there a performance requirement? A memory footprint requirement? An efficiency requirement? My usual advice is to create the simplest thing that works. This allows you (and others) to more easily understand the code and to avoid wasting your time on "optimizing" things that don't need to be optimal.

Use object-oriented programming

In this case, there's a thing called a "progress bar" that is expected to indicate the progress of some user-controlled process. This suggests that it might be better to create a ProgressBar object. That way, the current state of the bar, including the particular characters used, and its representation could be separate aspects of the same object. This would be much cleaner and also have the advantage of making it easier to create alternative output formats.

Consider the user(s)

Consider the user of this progress bar (presumably another programmer, or perhaps yourself). You might want to have some actual lengthy work done by the computer, and the progress bar is just an indicator. However, with the loop and usleep in the printProgressBar function, it's entirely possible that the progress bar itself would take up more time than the lengthy process that led the user to want a progress bar in the first place! There are a couple of ways to address this. The simplest would be to abandon the current look of the progress bar with its animation-like bar and adopt a simpler proportional bar. Another option would be to keep the existing visual effect, but to allow the percentage complete to be indicated by both how many stars are to the right, but also where the advancing star is within the empty space.

That leads to the next consideration, which is the other user of this code, which is not the programmer, but a person using the program that includes this progress bar. For that person, how clear is the intent of this progress bar?

Eliminate unused variables

This code declares main with arguments argc and argv in the usual way, but neither argc nor argv are actually used. This could be addressed by either declaring int main() or by modifying the code to actually use the variables.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks, but before accepting the answer, can you please point out what exactly i'm doing in a inconsistent way.<p>a \$\endgroup\$
    – BunnyGuy
    Commented May 22, 2020 at 22:01
  • 1
    \$\begingroup\$ The indentation within main, and specifically the while loop is probably not what you intended. Also, the two functions each have slightly different brace style (inline vs. start on next line). \$\endgroup\$
    – Edward
    Commented May 22, 2020 at 22:03
  • 1
    \$\begingroup\$ @BunnyGuy Also, the contents of the for loop in printProgressBar should be indented. \$\endgroup\$
    – pacmaninbw
    Commented May 23, 2020 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.