-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
have a method return a variable, or have it assign a value to a global variable? #87
Comments
In this particular case, it doesn't make much sense to have methods that do things this way. In the case of In terms of "principles" to follow (I'm sure there are official names for these principles, but I'll just give you my names ...):
|
Thanks, @bakercp! I read over your response and the wikipedia articles, both of which were helpful in beginning to answer my question, but perhaps more importantly in understanding why posting such abstract code isn't all that helpful! So please consider the point taken (and sorry about that.) I am going to build out, clan up and comment the example I'm actually working on and post it here, hopefully tonight. |
perfect :) On Mon, Oct 14, 2013 at 9:54 PM, Abraham Avnisan
|
Hey @abrahamavnisan, does this code get at your point? Again, it is meaningless code, but maybe have two alternative versions of code tackling the issue will help. The question is whether there is a general principle that would indicate to you whether the int variable beans should be globally defined (i.e. a class variable of testApp) or locally defined (to the update() method of testApp). beans is a variable local only to the update method: class testApp{
public:
void setup();
void update();
void draw();
int calculateNumberBeansFromMousePosition(int x, int y);
vector <ofVec2f> beanPositions;
};
void testApp::setup() {
}
void testApp::update(){
beanPositions.clear();
int beans = calculateNumberBeansFromMousePosition(mouseX, mouseY);
for (int i=0; i<beans; i++) {
// Do whatever to generate beans and add their positions to the vector
}
}
int testApp::calculateNumberBeansFromMousePosition(int x, int y){
return x*10.0f/y; // Or do whatever magic to calculate the number of beans
}
void testApp::draw(){
for (int i=0; i<beanPositions.size(); i++) {
// Do whatever drawing
}
} versus beans is a class variable and can be accessed anywhere within testApp: class testApp{
public:
void setup();
void update();
void draw();
void calculateNumberBeansFromMousePosition(int x, int y);
vector <ofVec2f> beanPositions;
int beans
};
void testApp::setup() {
}
void testApp::update(){
beanPositions.clear();
calculateNumberBeansFromMousePosition(mouseX, mouseY);
for (int i=0; i<beans; i++) {
// Do whatever to generate beans and add their positions to the vector
}
}
int testApp::calculateNumberBeansFromMousePosition(int x, int y){
beans = x*10.0f/y; // Or do whatever magic to calculate the number of beans
}
void testApp::draw(){
for (int i=0; i<beanPositions.size(); i++) {
// Do whatever drawing using the vector of positions
}
} I'll just add my two cents in addition to what Christopher has said. Encapsulation is the "proper" way to do things. You should only have global class variables when the variables are actually intended to be available anywhere within the class, like the velocity/position/acceleration of our particle classes. Otherwise, your variables should be local. Some reasons why:
But, global variables can make the writing your code go faster sometimes... You wouldn't have to remember to pass and return 'beans' to and from your various functions. When I'm trying to sketch out some code really quickly to see if any idea is worth exploring, the number of global variables I have defined explodes. Then l clean it up later. There have to be better examples to explain this side of it, but I'm blanking right now. So encapsulation is a good ideal - especially if it improves readability and robustness - but sometimes you temporarily (or permanently) bend the rules. I think this article echoes these points. I'm sure there must be plenty of discussions on stackoverflow global vs local scoping for variables. |
Or I should add, at least that's my experience, which very well could be silly and biased. |
Thanks so much for the detailed response, @mikewesthad! I've been busy making the code that the question is actually based on presentable but the quick look I took at it was super helpful. I'm looking forward to taking a closer look tomorrow, after some sleep! Hopefully throwing the actual code into the mix will help and not hinder the discussion. I've uploaded the code in question here - it might or might not evolve into an of poco/regex tutorial - please take a look and let me know what you think! There's an "intro" in the .h file comments, and there are comments throughout. Also, @bakercp, I finally (I hope!) mended the damage I had made on my repo, so take a look at the pull request when you have a minute and see if it looks OK to accept - I think it should be. (ps, @mikewesthad, you were totally right when you recommended I NOT use |
I'm confused about when I should have my methods return variables, as opposed to having my methods assign values to globally available variables. At least in certain cases, as in the example below, you can get the same result using both of these strategies, so which is better? Does the answer lie in the ever so mysterious passing by value / passing by reference?
Here is what I mean, broken down into a very simple example.
testApp.h:
testApp.cpp:
The text was updated successfully, but these errors were encountered: