• Beginner C++: Calculator
    38 replies, posted
[QUOTE=DementNeo;16083307]I would say at the top is best, but nullsquared did make portalized on the other hand so I don't wanna argue with his programming knowledge[/QUOTE] At the top is not best. The best is the most readable and easy to code, which is right before usage. "I see the user input is stored in the variable ch - I wonder what that is? Oh, it's right here: a character."
Each to his own "I see the user input is stored in the variable ch - I wonder what that is? I'll go look to the top where all the other variables are defined. After all this variable is used more than once, anyone reading my code will get confused when I re-use the variable later on if they haven't already read this part."
[QUOTE=nullsquared;16083682]At the top is not best. The best is the most readable and easy to code, which is right before usage. "I see the user input is stored in the variable ch - I wonder what that is? Oh, it's right here: a character."[/QUOTE] Instead of "I see the user input is stored in the variable ch - I wonder what that is? *scroll, scroll, scroll* Oh, a character! Now where were I? *scroll, scroll*.....*scroll, scroll, scroll* I'm sure it was around here... *scroll, scroll* Dammit!"
Ah, I see your point now. In my next programming session I shall do that.
[QUOTE=gparent;16083419]Scott Meyers' Effective C++ has an entry on this (Item 26); Not only does defining variables when you use them increase clarity by providing context, but it's also a potential performance increase for class variables with a non-trivial constructor when you define the variable inside a deeper scope, or past certain points in your function (return statements, throw statements, breaks, etc.) Basically you want to delay variable definition as much as you can.[/QUOTE] Isn't that saying as late as necessary, but as soon as possible? E.g. when the variable will get used in that scope, declare it as soon as possible, but if it only gets used in a certain scope or after a conditional return-statement (and similar) declare it after that. At least for non-trivial objects. [QUOTE=nullsquared;16083682]At the top is not best. The best is the most readable and easy to code, which is right before usage. "I see the user input is stored in the variable ch - I wonder what that is? Oh, it's right here: a character."[/QUOTE] Meaningful variable-names :)
[QUOTE=ZeekyHBomb;16084114]Isn't that saying as late as necessary, but as soon as possible? E.g. when the variable will get used in that scope, declare it as soon as possible, but if it only gets used in a certain scope or after a conditional return-statement (and similar) declare it after that. At least for non-trivial objects.[/QUOTE] No, it's not[b]*[/b]. he's basically saying "define variables when you need them, right where you need them." So say you have a function that has an if block unrelated to a variable used for input later on in the function, as such: [cpp] using namespace std; // for demonstration purpose void foo() { if (global->bar()) { doSome(); Thing(); } /* Generate some data */ /* ... */ /* Gather user input */ string input; getline(cin, input); /* Process that input */ } [/cpp] In the example above, even though my "if" block didn't contain an exit point, I still place "input" at the buttom of the function because that's where it gains context. That is, regardless of whether moving the variable will net a performance increase or not, I would still place the variable there. This is, according to me, the most obvious/efficient way of doing it. But since performance isn't an issue in this example, some people may prefer defining variables at the top. Either way, stick to one way of doing it and keep to it. One of my teacher insisted on having all definitions at the top, another insisted on the opposite, so whatever.< [b]*[/b]: Meaning, Scott Meyers isn't saying that. It's however possible that I worded his idea wrongly.
At the end of the day, it really doesn't matter where the hell you declare variables, as long as it's in the right scope. The only time it would matter is if you are doing c++ and you use a variable in a function before you declared it. It's the same as arguing over whether you should initialize variables when you declare them, or in the constructor. You get the same result either way. As for me, I just code stream of conscious style, so I have variables declared all over the place. If I know everything that needs to be done in a function, I'll declare them all at the top and then just go, and if I remember something I needed or need a new one, I just add it where the carat is. Then later on I'll reorganize it if I feel it's too messy.
[QUOTE=jivemasta;16087049]At the end of the day, it really doesn't matter where the hell you declare variables, as long as it's in the right scope. The only time it would matter is if you are doing c++ and you use a variable in a function before you declared it.[/QUOTE] We've just said that it's not only scope that matters. The place you put it in the function can also matter. [QUOTE=jivemasta;16087049]It's the same as arguing over whether you should initialize variables when you declare them, or in the constructor. You get the same result either way.[/QUOTE] What do you mean?
[QUOTE=gparent;16087805]What do you mean?[/QUOTE] I think it is like where you do int x = 5;
Sorry, you need to Log In to post a reply to this thread.