About using global vars

This is a discussion on About using global vars within the C Programming forums, part of the General Programming Boards category; Originally Posted by esbo I find it easy to track the effect of a vairable in a progran simply my ...

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by esbo View Post
    I find it easy to track the effect of a vairable in a progran simply my seaching on it in an editor. That is dead easy with a global variable, however when data is pass the name usually changes. This makes it much more difficult to follow like 10 times more difficult.

    The over use of local variable can make a program an absolute nightmare to follow.
    I have see it my self, the same variable changing names half a dozen times, it makes
    the program very difficult to follow.
    Do you not ever call your functions with different input? And for that matter, since functions can't change the value of the parameters passed into them, that's irrelevant for tracing purposes anyway.

  2. #17
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by tabstop View Post
    Do you not ever call your functions with different input? And for that matter, since functions can't change the value of the parameters passed into them, that's irrelevant for tracing purposes anyway.
    Yes I do call functions sometimes when appropiate but I have see programs which have be 'over engineered' into a mess because of an over empathsis on local variables.

    But the values passed into the function affect how the programs runs.
    It is simple to see what the effect of a variable is on a program by searching on it
    in an editor, however once it disappears into a function it 'disappears'.

    Most of the programs I have seen have been 'ruined' by 'good programming practise'
    in my opinion. The programmers lose all sense of proportion dogmatically following 'rules'.

  3. #18
    Registered User
    Join Date
    Apr 2006
    Posts
    2,009
    Quote Originally Posted by esbo View Post
    But the values passed into the function affect how the programs runs.
    It is simple to see what the effect of a variable is on a program by searching on it
    in an editor, however once it disappears into a function it 'disappears'.
    Yes, but you shouldn't need to look through code to see what a variable is supposed to do. The name, documentation, and general project design should make it clear what each variable does.

    If there is a bug, the problem is not with the variable, its with some function that doesn't do what its supposed to do. And functions are a lot easier to debug when you know by looking at the prototype what they can do and what they might change.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    2,009
    Quote Originally Posted by esbo View Post
    I find it easy to track the effect of a vairable in a progran simply my seaching on it in an editor. That is dead easy with a global variable, however when data is pass the name usually changes. This makes it much more difficult to follow like 10 times more difficult.

    The over use of local variable can make a program an absolute nightmare to follow.
    I have see it my self, the same variable changing names half a dozen times, it makes
    the program very difficult to follow.
    Have you tried using a debugger trace? It has the advantage of showing you what order a variable is used in, which can be much more useful than an editor search or a grep in showing program flow.
    Last edited by King Mir; 12-13-2008 at 05:08 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #20
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    Quote Originally Posted by esbo View Post
    It is simple to see what the effect of a variable is on a program by searching on it
    in an editor, however once it disappears into a function it 'disappears'.
    Or someone, besides you, could name their local variable the same as you named some global variable, and then the global 'disappears'. In C, local identifiers have precedence over a name with a wider scope.

    If you don't want to rename a variable several times, then don't, that's your prerogative. Why would you do that if you're working on projects alone anyway? That's not a reason to prefer globals.

    As soon as you work on a team though, abstraction becomes important so several people can build the software together, without breaking each others code inadvertantly, like through a naming clash, or by wanton use of some unrestricted value. That's really why lots of languages have some idea of scope in the first place.
    Last edited by whiteflags; 12-13-2008 at 05:31 PM.

  6. #21
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by King Mir View Post
    Yes, but you shouldn't need to look through code to see what a variable is supposed to do. The name, documentation, and general project design should make it clear what each variable does.

    If there is a bug, the problem is not with the variable, its with some function that doesn't do what its supposed to do. And functions are a lot easier to debug when you know by looking at the prototype what they can do and what they might change.

    Are you serious? "The name, documentation, and general project design should make it clear what each variable does. ".
    The variable does what it does, the documention etc is what is 'supposed' to do.

    What if there is no documentation?

    Most variable are not documented.
    The only thing which will tell you what a function does is the code.
    There may be errors in the design at all levels.

  7. #22
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by King Mir View Post
    Have you tried using a debugger trace? It has the advantage of showing you what order a variable is used in, which can be much more useful than an editor search or a grep in showing program flow.
    There may not be a debugger aavailable.
    What a 'trace' is is unclearly defined, there may well not be one which does what you
    just described, if indeed at all of if I even understand what you mean. (unclear).

  8. #23
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by whiteflags View Post
    Or someone, besides you, could name their local variable the same as you named some global variable, and then the global 'disappears'. In C, local identifiers have precedence over a name with a wider scope.
    I have never really ecountered that as a problem, people don't usually give different variables the same name in a waay which would cause confusion.

  9. #24
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    I have never really ecountered that as a problem, people don't usually give different variables the same name in a waay which would cause confusion.
    Well thanks for your opinion, but that is a serious drawback that you can't just ignore with some amount of hand-waiving (which seems to be what you're offering in general). What if the variable represents the same value (a function parameter, for instance, versus the argument)?

    Or in the context of my argument, you simply name the global poorly, and someone else names a different variable the same, but with a more restricted scope?

    Whatever else you could think of... Local variables make this a non-issue.

  10. #25
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by whiteflags View Post

    As soon as you work on a team though, abstraction becomes important so several people can build the software together, without breaking each others code inadvertantly, like through a naming clash, or by wanton use of some unrestricted value. That's really why lots of languages have some idea of scope in the first place.

    I have worked on on a team and not encoutered such problems, I am not even sure what you are talking about in 'abstractation' it's unclear.
    Your reasons are very wooly and unclear, "wanton use of some unrestricted value"?
    what the heck does that mean?

    Yes variables have a scope some global some local, it's a choice you do't have to try and
    make all the variables local, as some people do.

  11. #26
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by whiteflags View Post
    Well thanks for your opinion, but that is a serious drawback that you can't just ignore with some amount of hand-waiving (which seems to be what you're offering in general). What if the variable represents the same value (a function parameter, for instance, versus the argument)?

    Or in the context of my argument, you simply name the global poorly, and someone else names a different variable the same, but with a more restricted scope?

    Whatever else you could think of... Local variables make this a non-issue.
    I'm not really sure what you are talking about.
    Can you give an example?
    It all sounds like an non-issue to me.

  12. #27
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    Quote Originally Posted by esbo View Post
    I have worked on on a team and not encoutered such problems, I am not even sure what you are talking about in 'abstractation' it's unclear.
    Your reasons are very wooly and unclear, "wanton use of some unrestricted value"?
    what the heck does that mean?
    Ah, I see. Well 'abstraction' is an engineering principle that is applicable to software. In engineering, the idea is that if you build something like a machine, but you document the interfaces it uses. Then another layman can use it. As long as you use the correct cables, then the machine will work (even if you connect it to another machine!) without the person understanding how it works on the inside. That's a (rudimentary) definition of black box abstraction.

    The same engineering principle is used in software design to allow programmers to work on big projects without necessarily keeping the entire project in their head at once. In the context of C it has to do with how you design your functions, which would be essentially subprograms, with their own inputs and outputs. Since most functions will accept data, churn on it for a while and then make results, it helps to write those things down somewhere, so you can maintain the program (assuming you write anything useful enough to last a long time). I invite you to attend a virtual lecture for the book, The Structure and Interpretation of Computer Programs, if you don't want to hear it from me.

    If you do this as a way of life, then yeah, it's important. You can't work on commercial software without being at least a little organized, because the process is bigger than one person. And yeah, when people learn to program in school, good courses are designed to teach and have students practice these things, apart from rote memorization of language syntax.

    Or, if your functions don't return any specific data, then you would document (with comments if there is no formal paper) the inputs of the function; the state it leaves things it worked on; maybe a few example figures to demonstrate the process. These things are all helpful when you return to the program to maintain it, long after you've forgotten its impleentation details. In the same way, it helps others understand your design decisions and how the program works.

    wanton use of some unrestricted value
    You and I are working on a program together. You defined a global string to store input.
    In an uncharacteristic blunder this variable is
    char buffer[2048];

    I decide that I need my own buffer for whatever purpose. Perhaps I'm doing some parsing and need to maintain parts of the input. So I write a function. In it, I also name a variable buffer, and now the global is hidden from the scope of that function.

    Never underestimate how several people can lower the average IQ in a room.

  13. #28
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    All you really need to know are which variables are external to your code.
    These the variables which will interface to your code.
    If you were using such a buffer it would usually be given a sensible name eg global_buffer.

    You would probably have to declare it external to your code. It's not likely to be an issue.

  14. #29
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    Well frankly I'm not going to search for the one pathological case that will get you to agree with me. Even if you disagree, it is the way it is. It sounds like to me that you cannot accept what you prefer is rather against the grain, especially since the language you use relies on scope to understand what an identifier means and it's type (well, at least when the compiler looks for a declaration).

    Given your bias, you would do yourself a service to reach for an early version of BASIC whenever you need to write something

  15. #30
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by whiteflags View Post
    Well frankly I'm not going to search for the one pathological case that will get you to agree with me. Even if you disagree, it is the way it is. It sounds like to me that you cannot accept what you prefer is rather against the grain, especially since the language you use relies on scope to understand what an identifier means and it's type (well, at least when the compiler looks for a declaration).

    Given your bias, you would do yourself a service to reach for an early version of BASIC whenever you need to write something

    It is really not a problem for me at all.
    It might be for everyone else but that's their problem not mine.
    I can easilly write code to fit into a big project.
    If there are any scoping problems they will be easy enough to sort out.
    It's not something I would want to waste time on beforehand.

Page 2 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 11:02 AM
  3. Maintaining Global Vars Between Libraries
    By Canadian0469 in forum C Programming
    Replies: 9
    Last Post: 11-27-2007, 11:29 AM
  4. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 07:37 AM
  5. Global Vars
    By ihsir in forum C++ Programming
    Replies: 4
    Last Post: 04-18-2002, 10:40 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21