General questions about style, syntax and best practices.
I recently finished an introductory course to c++ at my college. The course covered the basics, it started with variables, data types, control structures, functions and ended with a brief glimpse at structs.
It didn't cover classes or any type of object oriented programming.
After finishing the final project I was left with many questions about different general aspects that only emerged after having to deal with the real problem solving puzzle that the project itself represented.
These are some of them, I was hoping one you guys could shed some light to them:
1- variable scope: is it OK to declare variables in different sections of a function, where its use is pertinent, or must all function variables be global to that function. ?
2- - Is it OK to use global constant variables to enlighten the meaning of function arguments, when calling that function. As opposed to using just numbers or values. ?
e.g :
Code:
...
const bool VERBOSE = true;
...
copyFiles(VERBOSE);
instead of:
copyFiles(true);
As the name implies, verbose makes copyFiles(); provide information of what it's doing.
3- Where should default argument values be properly declared, in the function prototype or in the function definition. ?
4- Code repetition: I am under the impression that repeating the same instruction over and over when different conditions are met, is inefficient and requires more typing work. So I try to avoid it. However I often find myself in situations where I waste much time trying to figure out how to restructure a whole section of code so I don't have to repeat a line many times.
Besides wasting my time and calories, this distracts me from the general bigger picture of the program, and I lose valuable design ideas I had. Moreover, more often than not I end up with a half-assed solution that consists of using a variable if the condition is met, to trigger the line(s) of code that I would have to repeat.
The problem is that I end up having to repeat the new line assigning the var to a certain value, as many times as before, plus, now I need to add a check for the variable to have that value, before triggering the previous line of code I wanted to avoid repeating in the first place. If there is just one line needing repetition, is it ok to go ahead and copy it if it can't be avoided, or is the solution depicted above still a better alternative ?
e.g: Here is an excerpt of my final project were I encountered a similar situation,
Code:
if (someConditionIsMet)
{ message ("g_over");
game_over = true;
}
else if (someConditionIsMet)
{ message ("noAmmo");
game_over = true;
}
else if (someConditionIsMet)
{ message ("ranAway");
game_over = true;
}
if (game_over)
{ message ("exit");
initialize();
}
As opposed to:
if (someConditionIsMet)
{ message ("g_over");
message ("exit");
initialize();
}
else if (someConditionIsMet)
{ message ("noAmmo");
message ("exit");
initialize();
}
Where I repeat message ("exit"); and initialize(); over and over.
Many thanks.