TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: AARE TALI
from: DANIEL JONES
date: 1997-08-13 05:06:00
subject: Sunir ventures into C++

AT>  > AT>    I don't like the idea too much. When you find some variabl
AT>  > AT> the program, you can't find declaration in few logical steps
AT>  > I'm not sure I follow you here.  When you find a
AT>  > variable, the
AT>  > declaration should be right there.  No steps are
AT>  > necessary.  No paging
AT>  > back to the top of the page or function.
AT>     void foo()
AT>     {
AT>         int i;
AT>         for (i = 0; i < 5; i++)
AT>             do_something(i);
AT>         for (int j = 0; j < 5; j++)
AT>             do_something_else(j);
AT>         if (i < 5)
AT>             not_everything_was_done_with_i();
AT>         if (j < 5)
AT>             not_everything_was_done_with_j();
AT>     }
Did you test this? If so, with what compiler?
With BC++ 5.01, I get an error:
Undefined symbol 'j'
Why?  Because the for() loop defines a block.  j is local to that block,
and is out of scope in your "if(j < 5)" statement.  As I recall, this was
a matter of some debate, and wasn't implemented in early versions of
C++.  I'm reasonably certain that BC++ 3.1 allowed the above code.
Even if the code was (still) legal, however, it doesn't really prove
anything.  I can post all kinds of code mis-using pointers, for example,
but that isn't an argument for their abolishment.
Bottom line: it's up to the programmer to write their code in a manner
that's readable and maintainable.  Declaring variables within code can
either assist or hinder that goal.  It primarily depends upon the
discipline of the programmer.
AT>    In C you have to _go to_ the beginning and look around there, in
AT> have to _scan_ looking around all the way. Unless you write 5-line-f
AT> where you can't put declaration smore than 5 lines away from the lin
AT> used the variable last time.
Most of my functions are fairly small anyway.  If it's a variable that's
used throughout the function, I normally declare it at the top, along
with a brief comment stating it's use.  If it's a temp variable, such as
a loop counter, I declare it as I need/use it.  I find this method
comfortable, intuitive and easy to use.
If you're comfortable declaring all your variables, even unimportant,
temporary counters and such, at the entrance of your functions, by all
means do so.  Your functions will compile and work just fine.  And so
will mine. Ain't it a beautiful thing?
Regards,
Daniel              ddjones@pinn.net
---
 þ RM 1.31 1604 þ Fundamentalism: Fund=give cash; amentalism=without brains.
---------------
* Origin: Selective Source Virginia Beach, VA (757)471-6776 (1:275/102)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.