Thread: guys i really need help! else if ..

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,407
    Quote Originally Posted by quzah
    It is easier for me to locate counter where I expect it (at the top of the current block, or the top of the function) than to have to hunt for it on every single one of those 100 lines.
    Actually, you still have the problem of locating the point where counter was last assigned a value. But in this case, I'd say the problem really is:
    Quote Originally Posted by \007
    Code:
    /* 100 lines later */
    With shorter functions that do one thing and do it well, the problem of locating the declaration disappears, and declaring variables at the top is less problematic because the scope is already reduced.

    Quote Originally Posted by quzah
    See, you aren't even doing it right now:
    Agreed, unless there is a special reason to do so, the scope of i should not extend beyond the loop.

    Quote Originally Posted by Matticus
    If this causes confusion or "error because I may have assigned it to something else between here and up there," then that just means I would have to correct and learn from that error.
    Prevention is better than cure.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Quote Originally Posted by quzah View Post
    I don't. But you could, and that is the whole reason for my dislike of the ability to do so.It is easier for me to locate counter where I expect it (at the top of the current block, or the top of the function) than to have to hunt for it on every single one of those 100 lines.

    See, you aren't even doing it right now:You should be doing:
    Code:
    for( int i = 0, counter = 10; i < counter; it++ )

    Quzah.
    Yes, I rather place it inside the loop's scope but C89 wouldn't allow that. It was just an overly simple example.

  3. #33
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by \007 View Post
    Yes, I rather place it inside the loop's scope but C89 wouldn't allow that. It was just an overly simple example.
    C89 wouldn't let you do what you did before that either:
    Code:
    int main(void)
    {
      /* 100 lines later */
    
      int i, counter = 10;
      for(i = 0; i < counter; i++)
        printf("%d\n", i);
    
      return 0;
    }
    You can't do that in C89 unless right before there, you start a new code block - and then if you are doing that, you are doing it the "old style" anyway, so your argument is self defeating.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thanks Guys...
    By frodonet in forum C Programming
    Replies: 0
    Last Post: 11-03-2007, 08:16 PM
  2. do you guys have...
    By enjoyincubus in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 10:14 PM
  3. Hello guys =)
    By carol.prime in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-22-2006, 10:25 AM
  4. What do you guys think about this?
    By RobR in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 06-07-2006, 11:17 AM
  5. Hey guys, I'm new!
    By MrDoomMaster in forum C++ Programming
    Replies: 15
    Last Post: 10-31-2003, 05:47 PM