Coding Conventions

Pages: 12
@Desh: Indeed. I use recursion from time to time as it is a very powerful feature.

However, with loops, it's not nearly as easy to slip up. We've had a lot of problems over the years with people using recursion and making a simple mistake that got them a buffer overflow.

-Albatross
Last edited on
But then again, i could imagine that recursion is disliked especially here on the 'beginner' section, if you get me right ;)
closed account (oE6pX9L8)
@Galik:

1
2
3
4
using std::cout;
using std::endl;
using std::vector;
using std::string;


I like this idea, but using namespace is probably better for what I'm doing at the moment (i.e. simple programs).

@Athar:

[i]COUNTER should definitely not exist globally. You should declare variables as locally as possible. So in this case, inside the for statement. The same thing applies to CHOICE. It is very unusual to actually name a counter variable COUNTER. Instead lower case letters are used (usually i and j, k, l ... for nested inner loops)[i]

Is there any glaring issues with globally declaring variables? Does it slow programs down, or is it just a bad habit?

I tried it as this:

1
2
for (int COUNTER...)
for (etc.)


But I got some kind of compile error.

I prefer naming my variables with more description than necessary. Is that a bad thing?

archmage84

PS: Is overusing bold a bad thing?
Descriptive variables are never bad! Name them however you want aslong as it makes understanding them easier, capitalized or not - whatever floats your boat :)

When it comes to global variables, it is not ALWAYS a bad habit, but can easily become in larger projects where you have many variables to keep track of - limiting their scope not only increases readability of your code, it also makes it safer :)

Hope that helped.
Last edited on
closed account (z05DSL3A)
archmage84 wrote:
I like this idea, but using namespace is probably better for what I'm doing at the moment (i.e. simple programs).

When you know a bit more about namespaces and scopes, read up on using declarations and using directives. That way you can make a more informed choice on which to use and the possible ramifications of one over the other.
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.
Last edited on
C and C++ programmers typically expect that ALL_CAPS identifiers represent macros or constants.
Topic archived. No new replies allowed.
Pages: 12