locked
Too many Global Variables RRS feed

  • Question

  • Hi Everyone,

     

    I am working on a B/S project. If I create too many Global Variables, will it cause any issues?

     

    Thanks,

    Bo

    Monday, November 21, 2011 4:18 AM

Answers

  • If a variable is globally scoped when it shouldn't be then it is consuming memory when it shouldn't be.  If the total size of the unnecessary variables is large, you could be wasting a lot of memory.  That said, it would take a LOT to use up enough to really be noticable.

    The biggest problem is just with design.  It's harder to debug variables that are global when they shouldn't be.  It results in poor programming habits that make programs harder to understand and maintain, etc.

    It's well worth the time to determine what scope a variable ought to have, and to make it work with that scope.  If in doubt, use a smaller scope than you think you need and increase it if you have to.  It's much easier to increase a variables scope than to decrease it.

    • Proposed as answer by JMCF125 Monday, November 21, 2011 8:43 PM
    • Marked as answer by Bob Shen Thursday, December 1, 2011 3:17 AM
    Monday, November 21, 2011 7:32 PM

All replies

  • What do you mean by global variables? member fields? If yes, why that will cause any issues? Just give proper accessibility to the members to ensure they are accessed properly. Please forgive me if I have understood your problem incorrectly.
    Please mark this post as answer if it solved your problem. Happy Programming!
    Monday, November 21, 2011 5:11 AM
  • If a variable is globally scoped when it shouldn't be then it is consuming memory when it shouldn't be.  If the total size of the unnecessary variables is large, you could be wasting a lot of memory.  That said, it would take a LOT to use up enough to really be noticable.

    The biggest problem is just with design.  It's harder to debug variables that are global when they shouldn't be.  It results in poor programming habits that make programs harder to understand and maintain, etc.

    It's well worth the time to determine what scope a variable ought to have, and to make it work with that scope.  If in doubt, use a smaller scope than you think you need and increase it if you have to.  It's much easier to increase a variables scope than to decrease it.

    • Proposed as answer by JMCF125 Monday, November 21, 2011 8:43 PM
    • Marked as answer by Bob Shen Thursday, December 1, 2011 3:17 AM
    Monday, November 21, 2011 7:32 PM