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:
- How would you rate my code, what to improve ?
- how to improve his readbility?
- this code would be fine to work with or a headache to mantain, how to improve?
- is there Better ways ( that are not so complex too ) to achieve the same ? with the star moving and everything.
- Is there a best practice i'm not following or missing ?
- i hardcoded some values is that bad ?
- 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.
1 Answer 1
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.
-
\$\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– BunnyGuy2020年05月22日 22:01:56 +00:00Commented May 22, 2020 at 22:01
-
1\$\begingroup\$ The indentation within
main
, and specifically thewhile
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– Edward2020年05月22日 22:03:21 +00:00Commented May 22, 2020 at 22:03 -
1\$\begingroup\$ @BunnyGuy Also, the contents of the for loop in
printProgressBar
should be indented. \$\endgroup\$2020年05月23日 03:08:18 +00:00Commented May 23, 2020 at 3:08