Too many variables a problem?

G

G-Factor

Hi all

Been learning C++ for about 5 weeks. I find that I create quite a lot of
variables. Generally, is it better to create a new variable for every
different thing that needs to be stored in the program? Or would you reuse
variables in the code... like declare an 'int i', and use it in loops,
storing temp values, storing main variables before you output it to
screen...then put something else in it later if you need an int again.

Thanks

G-Factor.
 
J

John Harrison

G-Factor said:
Hi all

Been learning C++ for about 5 weeks. I find that I create quite a lot of
variables. Generally, is it better to create a new variable for every
different thing that needs to be stored in the program? Or would you reuse
variables in the code... like declare an 'int i', and use it in loops,
storing temp values, storing main variables before you output it to
screen...then put something else in it later if you need an int again.

Thanks

G-Factor.

Most newbies create too many variables, but the answer isn't reusing
variables, that's confusing. The answer is to realise that you don't need
the variables in the first place.

For instance typical newbie code is

int x = something();
cout << x;

You should prefer

cout << something();

Why not post a piece of code you think has too many variables and we'll see
what we can do to clean it up.

john
 
P

ppu

G-Factor said:
Hi all

Been learning C++ for about 5 weeks. I find that I create quite a lot of
variables. Generally, is it better to create a new variable for every
different thing that needs to be stored in the program? Or would you reuse
variables in the code... like declare an 'int i', and use it in loops,
storing temp values, storing main variables before you output it to
screen...then put something else in it later if you need an int again.

Thanks

G-Factor.

I believe scope is a good way to handle this. For example,
look below:

* original

int x = something();
int y = somethingElse();

doStuff(x,y);

int p = whatever();
int q = whateverElse();

doWhatever(p,q);

* with scope

{
int x = something();
int y = somethingElse();

doStuff(x,y);
}

{

int p = whatever();
int q = whateverElse();

doWhatever(p,q);
}

What happens here is you make it abundantly clear to
the person looking at the code (and the compiler), that
you only need x and y for a certain period of time, which
means the stack can be re-used efficiently later on.

Some people don't like this style, but I find it extremely
good on the eyes and makes it easy to read.

caustik
 
K

Karl Heinz Buchegger

John said:
There is a third way

doStuff(something(),somethingElse());
doWhatever(whatever(),whateverElse());

which is exactly the point I was making about unnecessary variables.

<nitpicking_and_I_know_that_John_knows_this._Just_want_to_point_it_out_for_newbies>

But this is not allways possible. It assumes that eg. something() and somethingElse()
are not dependent on each other. In other words: they can be alled in any order
and still give the same result.

In C++ the order of argument evaluation is unspecified. This means it is up
to the compiler, if something() is called before somethingElse() or the
other way round.

</nitpicking_and_I_know_that_John_knows_this._Just_want_to_point_it_out_for_newbies>
 
M

Michael Kochetkov

G-Factor said:
Hi all

Been learning C++ for about 5 weeks. I find that I create quite a lot of
variables. Generally, is it better to create a new variable for every
different thing that needs to be stored in the program? Or would you reuse
variables in the code... like declare an 'int i', and use it in loops,
storing temp values, storing main variables before you output it to
screen...then put something else in it later if you need an int again.
Just my opinion. We usually use different variables for different entities.
The approach when in a scope an int variable means weight, speed or miles in
different places is considered to be error prone. You shall be very
attentive and do not forget to initialize your variable the proper way each
time you are going to use it. For a company programmers time and
qualification are resource that cost money. There are situations when
several people work on the same code. It is usually considered as a plus if
they work the most efficient way. If all the variables have "speaking" names
then it is easier to use them, programmers may concentrate on the task and
spend less time on overhead charges, like investigation of what the concrete
name mean in this very place. Here goes the quotation from Uncle Scrooge
(something like money that are saved are earned money -- I do not know it in
English).
And in general, the simpler code to understand and to maintain the more
reliable it is. The proper names for variables make the code simpler, IMO.
 
S

stephan beal

G-Factor said:
Been learning C++ for about 5 weeks. I find that I create quite a lot of
variables. Generally, is it better to create a new variable for every
different thing that needs to be stored in the program? Or would you reuse
variables in the code... like declare an 'int i', and use it in loops,
storing temp values, storing main variables before you output it to
screen...then put something else in it later if you need an int again.

After you've had to maintain some else's code, where (s)he re-used a
variable in completely different contexts (especially if it's a global
var), you will:

a) learn a few new swear words.
b) learn to not re-use vars in multiple contexts (unless it was designed
explicitely for that purpose, and duly noted in the API docs).

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.
 
J

John Harrison

Karl Heinz Buchegger said:
<nitpicking_and_I_know_that_John_knows_this._Just_want_to_point_it_out_for_n
ewbies>

But this is not allways possible. It assumes that eg. something() and somethingElse()
are not dependent on each other. In other words: they can be alled in any order
and still give the same result.

In C++ the order of argument evaluation is unspecified. This means it is up
to the compiler, if something() is called before somethingElse() or the
other way round.

</nitpicking_and_I_know_that_John_knows_this._Just_want_to_point_it_out_for_
newbies>

I wouldn't call it nitpicking, its something I should have pointed out
myself.

john
 
G

G-Factor

Been learning C++ for about 5 weeks. I find that I create quite a lot of
variables. Generally, is it better to create a new variable for every
different thing that needs to be stored in the program? Or would you reuse
variables in the code... like declare an 'int i', and use it in loops,
storing temp values, storing main variables before you output it to
screen...then put something else in it later if you need an int again.

Thanks for all your help guys, makes a lot more sense to me now

G-Factor
 
D

David Cattarin

G-Factor said:
Hi all

Been learning C++ for about 5 weeks. I find that I create quite a lot of
variables. Generally, is it better to create a new variable for every
different thing that needs to be stored in the program? Or would you reuse
variables in the code... like declare an 'int i', and use it in loops,
storing temp values, storing main variables before you output it to
screen...then put something else in it later if you need an int again.

Thanks

G-Factor.

The answer is, of course: It depends.

It sounds like you think variables are expensive in some way. They
typically aren't (unless they are expensive objects ;). So, too many
variables is not really a problem. What matters is how clear your code
is and whether the number of variables you have helps or hinders that
clarity. It is also a matter of personal preference, where some
programmers try to eliminate as many variables as they can.

Now, if you are thinking about performance and how variables map to
registers, I'll suggest that you not to think about that. That is the
compiler's and optimizer's job and it is very doubtful that you'll
convice the optimizer to do a better job by reusing variables. In
fact, if you are reusing variables across code blocks you'll most
likely get less optimized code--of course, that depends on many things
too.

All that being said, my advice is to use one variable to do one
"thing" and to name it appropriately. Remember RAII.

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top