bad side of global variable
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: bad side of global variable

  1. #1
    Join Date
    Dec 2013
    Posts
    19

    bad side of global variable

    here board id global variable
    Take the game board example from earlier—you might decide to create a function to display the board and have that function access a global variable. But what happens if you want to display some board other than the current board—for example, to show an alternative move? Your function doesn’t take the board as an argument; it shows only the single global board. Not very convenient!

    could any one detial what is said here.it will be more good if you do with example code.please

  2. #2
    Join Date
    Apr 1999
    Posts
    27,444

    Re: bad side of global variable

    Quote Originally Posted by bhuvaneshnick View Post
    here board id global variable
    Take the game board example from earlier—you might decide to create a function to display the board and have that function access a global variable. But what happens if you want to display some board other than the current board—for example, to show an alternative move? Your function doesn’t take the board as an argument; it shows only the single global board. Not very convenient!

    could any one detial what is said here.it will be more good if you do with example code.please
    Code:
    struct Board
    {
    };
    
    Board myBoard;
    
    void MovePiece(int x, int y)
    {
        //...move piece on myBoard, since this function only knows about myBoard
    }
    
    Board myBoard2;  // this is a second board
    
    MovePiece(5, 6);  // moves piece on board #1
    
    // How are you going to have the same MovePiece function move a piece on the second board?  You're stuck.

    Code:
    struct Board
    {
    };
    
    void MovePiece(Board& b, int x, int y)  // function takes a board, and moves a piece on that board
    {
      //....
    }
    
    Board myBoard;
    Board myBoard2;
    //...
    
    
    MovePiece(myBoard, 5, 6);  // moves piece on board #1, independent of board 2
    MovePiece(myBoard2, 10, 12);  // moves piece on board #2, independent of board 1
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 18th, 2013 at 09:47 PM.

  3. #3
    Join Date
    Dec 2013
    Posts
    19

    Re: bad side of global variable

    i am beginer to c++ till now i am not crossed datastructures and classes.you are above example comes in that category.please sir will say this without struct and classes.i know its baby question.even if you dont reply for this so MUCH THANK YOU FOR FORWARDING I CAN UNDERSTAND sir

  4. #4
    Join Date
    Apr 1999
    Posts
    27,444

    Re: bad side of global variable

    Quote Originally Posted by bhuvaneshnick View Post
    i am beginer to c++ till now i am not crossed datastructures and classes.you are above example comes in that category.please sir will say this without struct and classes.
    The issue has nothing to do with struct or classes.

    Did you read the comments in the code? Do you see the difference between MovePiece in the first code sample and MovePiece in the second sample? Which one of the samples is the only one that would seem to work for 2, 3, or any board, and not just one single, global board? You don't need to know what a struct or class is to understand this.

    Regards,

    Paul McKenzie

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,202

    Re: bad side of global variable

    The original post seems like it's stated pretty simply. Which part don't you understand?

  6. #6
    Join Date
    Dec 2013
    Posts
    19

    Re: bad side of global variable

    i understand sir thanks for forwarding.will you say what are the other evil sides of decalring global variable

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,202

    Re: bad side of global variable

    What the original post is saying is that if you have a global variable that represents the state of something, in the case provided it's a game board, then unless you want to jump through a lot of hoops, you're restricted to only having one of that thing.

    Another disadvantage is it's harder to maintain control of the variable as it can be accessed and modified from anywhere in the program. That may not sound like a big deal, but it makes programs hard to manage and debug.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: bad side of global variable

    Quote Originally Posted by bhuvaneshnick View Post
    i understand sir thanks for forwarding.will you say what are the other evil sides of decalring global variable
    1) Unit testing. A given function should always behave the same way when called with the same arguments; this is the basis of unit testing. If that function relies on global state, then it may not have this property. (Global constants are fine.)

    2) Thread safety. This won't mean much for a beginner, but once you get to multithreaded programming, you'll need to carefully control when variables get modified and which threads have access to them. Global variables make this more difficult.

    3) Maintainability. When someone new joins your team and tries to use your code, it's a lot easier to figure out what a given function is supposed to do if its inputs and outputs are clear. Reading global variables means your function has "hidden" inputs, and writing global variables means your function has "hidden" outputs. This can make it more difficult to understand the code structure.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center