archmage84 wrote: |
---|
I prefer naming my variables with more description than necessary. Is that a bad thing? |
In general, that is actually a good thing.
Not in the case of counter variables though. Using i as the counter variable name is one of the most universally accepted style rules and disregarding that rule will make your code harder to read for other people.
However, in the case of nested loops using descriptive counter variables instead of i, j, k... can make the code easier to read.
Is there any glaring issues with globally declaring variables? Does it slow programs down, or is it just a bad habit? |
Global variables make sense in some cases, namely when you need to use a certain object in various functions, but don't want to pass the value as a parameter every time.
But in your case that doesn't apply. You don't use COUNTER or CHOICE to share any information. They're only used locally in various functions and therefore should be declared there. This is far more than just a bad habit: since the COUNTER variable is shared between all functions, using more than one thread would result in complete disaster. Actually, even one thread can be sufficient: when calling another function from inside the loop that also uses the COUNTER variable, you have a problem. Two more, although far less severe issues are that you can't immediately see what type COUNTER has and that it uses memory during the whole lifetime of the program.
Your Starline function should look like this:
1 2 3 4
|
for (int i=0;i<PRE_ENDL;i++) cout << endl;
for (int i=0;i<WHITESPACE;i++) cout << " ";
for (int i=0;i<STAR_COUNT;i++) cout << "*";
for (int i=0;i<POST_ENDL;i++) cout << endl;
|
It's better to start counting at 0, as you will have to do that anyway when accessing arrays or other containers in the for loop. No point starting at 0 half the time and at 1 the rest of the time, especially since you provoke accidental off-by-one errors that way.
Edit: since std::string has a constructor that creates a string with the length n consisting of the character c, you could also write the function as:
cout << string(PRE_ENDL,'\n') << string(WHITESPACE,' ') << string(STAR_COUNT,'*') << string(POST_ENDL,'\n') << flush;
Your version is more intuitive and easier to read, though.