-
December 18th, 2013, 03:50 PM
#1
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
-
December 18th, 2013, 09:44 PM
#2
Re: bad side of global variable
Originally Posted by bhuvaneshnick
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.
-
December 18th, 2013, 10:44 PM
#3
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
-
December 19th, 2013, 12:48 AM
#4
Re: bad side of global variable
Originally Posted by bhuvaneshnick
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
-
December 19th, 2013, 08:14 AM
#5
Re: bad side of global variable
The original post seems like it's stated pretty simply. Which part don't you understand?
-
December 19th, 2013, 10:06 AM
#6
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
-
December 19th, 2013, 10:29 AM
#7
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.
-
December 19th, 2013, 01:14 PM
#8
Re: bad side of global variable
Originally Posted by bhuvaneshnick
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|