Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. The Lounge
  3. Code Neatness

Code Neatness

Scheduled Pinned Locked Moved The Lounge
c++question
82 Posts 42 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N nalorin

    Steve Echols wrote:

    but if you try that in C++ you get a duplicate definition error on i.

    I've never had that problem, and I don't see why anyone would... // "i" does not exist at this line for (int i=0; blah; blah) { do something } // "i" exists // "i" does not exist for (int i=0; blah2; blah2) { do something else } // "i" exists // "i" does not exist In the above code, "i" in each for() loop is in a different scope - i will be taken off the stack right after the last iteration of the first for() loop, and will be recreated on the stack for the second for() loop - only to be removed from the stack again when the second for() loop is done its last iteration. You should try it. Try running this code: *edit - converted angle brackets to &xx;'s* #include <iostream> using namespace std; int main () { for (int i=0; i<10; i++) cout << "counting...\n"; cout<< "i = " << i << endl; return 0; } I can assure you that you'll get a compile error saying that "i" is out of scope or obsolete or something like that... if you don't you should get a newer compiler ;)

    "Silently laughing at silly people is much more satisfying in the long run than rolling around with them in a dusty street, trying to knock out all their teeth. If nothing else, it's better on the clothes." - Belgarath (David Eddings)

    Steve EcholsS Offline
    Steve EcholsS Offline
    Steve Echols
    wrote on last edited by
    #81

    Maybe it's just VC6, haven't tried it in VS2k3 or 2k5. Someone above mentioned that there's an option you can set to scope it properly.


    - S 50 cups of coffee and you know it's on!

    • S
      50 cups of coffee and you know it's on!
      Code, follow, or get out of the way.
    1 Reply Last reply
    0
    • R Rick Shaub

      According to Scott Meyers, author of "Effective C++: 55 Specific Ways to Improve Your Programs and Designs" you should "Postpone variable definitions as long as possible" (p 113) He actually dedicates a complete chapter to the subject. The reasoning for posponing declarations is to avoid incurring unnecessary construction overhead. This mostly applies to functions that have branching that may prevent all code segments from being executed. I personally don't think that declaring variables in the code body contributes to or against readability.

      M Offline
      M Offline
      mbrezu2
      wrote on last edited by
      #82

      If I remember correctly, "Code Complete" recommends declaring variables as close to their usage (better locality). But it also recommends using variables for just one purpose... So if you're factoring out common stuff and have little functions/methods that do only one thing, there is good locality regardless of using a consolidated style of variable declarations or not.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups