Thread: esbo's data sharing example

  1. #46
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by robwhit View Post
    Why are you yelling at me? Well... hmm. Actually, no. That seems really odd. You would think that they would have proof somewhere.Thanks esbo!

    I couldn't find any with no big words though...

    Here are some that are some of the reasons given to me:

    http://en.wikipedia.org/wiki/Global_variable
    http://www.infis.univ.trieste.it/C/N...tion3_6_3.htmlhmm... maybe. I don't think I've thought about it quite that way before. I'm not really that good at math.

    And come to think of it, I'm always having problems with extensibility in my programs. If it's not one thing, it's another. If it's not that, it's code in spot C. There's so many things that manage to go wrong. Isn't there an easier way?

    I think you may be on to something.
    http://en.wikipedia.org/wiki/Global_variable
    "The use of global variables makes software harder to read and understand. Since any code anywhere in the program can change the value of the variable at any time, understanding the use of the variable may entail understanding a large portion of the program"

    OK lets take this 'gem'.

    It is perfectly easy to see where the variable is used in the program, you just do a search on it. The beauty of global variables is that they can be modified anywhere, it is not a failing it is a strength. Only takes a few clicks of the mouse to see where the variable is used.


    Futhermore to say that local variables are easier to understand is wrong, local variables
    can change other variables in the program via return values. Not only that it could change
    any variable of the same type throughout the program.
    Now if you want to find out how that local variable affects the rest of the program you have to look at all the calls to that function and various different variables affected by that call,
    so now you have to do multiple searches on different variable names!! Futhermore those variables will also be passed
    into other functions affecting other variables. You effectively could have a chain reaction.
    A nuclear explosion of complexity taking place in the program!!!

    Most of the 'bad practice' I see is people trying to make global variables local!!!!
    You see some horrendous things, great wadges of data beinig passed around, the names constantly changing, making it almost impossible to keep track of what is going on.

    I have seen stuff being passed down through 4 or 5 levels of functions, the name changing each time. In making their program 'less complex' and easy to follow they create the biggest labyrinth of complexity ever seen.

    If you want to make you program imcomprehensible avoid using global variables.
    Mind you when you come back to it in a few months you won't be able to undestand it either

  2. #47
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Ok, interesting point of view. Before I respond to your post, I have a few questions:

    How do you make re-entrant functions?

    How do you avoid name conflicts?
    Last edited by robwhit; 01-08-2008 at 10:57 PM.

  3. #48
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by anon View Post
    Do you consider statistics as valid math?

    How many lines of code would you have to change to introduce another array and save it to a different file?

    How many lines of code would you generally have to change to reuse parts of the code of a fairly large progject in another project if all or most of your functions depend on each other via global variables and everything is hard-coded for a particular scenario?

    How many bugs would changes or attempts at reuse introduce if all your functions work by having curious side-effects? Etc.
    It would be trivial to introduce another array and save it to a different file.
    I could do it in less than a minute, so it's not something I would lose sleep over.


    Quote Originally Posted by anon View Post
    --------
    Globals generally seem easier to novice programmers, as well as most other bad practices (spaghetti code, magic values), because they let one write small programs without much thought to structure. A thought occurs and you write the code.
    With experience however, you should be able to recognize solutions concerning better structure etc more easily. (Unless, of course, you are satisfied with what you know and decide you can stop developing.) Although I am a hobbyist, it does not require me any effort to determine what would reasonably be the arguments and return value of a function that saves an array to a file, and I'm pretty sure most programmers feel the same.
    So, you use globals effortlessly and can't reuse functions in different projects or even in the same program with different data (at least not without a tedious copy'n'paste job). I wouldn't use global data, would write the same functionality just as effortlessly with parameters and return value, and be able to reuse the code (within reason).
    Both functions are only used once so it is pointless making them multipurpose.
    I may never use the function again, I cross bridges when i get to them not before.

    What happens is a novice programmer discovers how to use local variables and then
    use then excessively because he now an 'experienced programmer'.
    Chance are you will never use the function again so you are just creating work for yourself
    and making your program unnecessarilly complicated. Every variable you pass is an oppertunity to make an error. get the types wrong and you will have some nice exotic bugs
    which could lay dormant for some time.


    Quote Originally Posted by anon View Post
    As an example of your bad structure, you are mixing completely different jobs in your functions. datasave() cannot possibly know whether it is called in a context where exiting is a valid error response.
    Yes it can, it is only called in one context.

    Quote Originally Posted by anon View Post
    Or the straight function, that instead of returning a result prints the result. That is, the function makes an unjustified decision what it should be used for.
    It prints the result so I know that the code is working.
    If I returned a value I could make a mistake, it is an opertunity to make an error.
    The code might actually work but the error could be in the extra complexity of returning
    a value.



    Quote Originally Posted by anon View Post
    The thing with this design and globals is that small changes are not localized to a single or a few places but may be required all over the place.
    Any change can affect any part of a program, it does not matter whether the variables
    are global or local it depends on the overall design of the program, not the scope of
    the variable.
    The scope only affects where the variable can be changed, it does not restrict how those
    changes might affect the overall program.

    I have a 1000+ line program it has 17 local variables, about 80 global variables, and 6
    functions, one of which requires 2 arguements. None of the functions return any values.
    It works fine and is easy to follow.
    I don't find any problem whatsoever with the global variables. Infact it is the function which
    is passed values which is hardest to follow

  4. #49
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by robwhit View Post
    Ok, interesting point of view. Before I respond to your post, I have a few questions:

    How do you make re-entrant functions?

    How do you avoid name conflicts?

    "How do you make re-entrant functions?"

    Easy, I don't!!!!!!!!!! (shock horror!!!!!!!!)

    "How do you avoid name conflicts?"

    That's a bit more tricky, after hours of sleepless nights I decide to give the variables different names, a stroke of genius don't you think?

    I tried using the same name for every variable but I eventually gave up on it as a 'lost cause'.

  5. #50
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I can not believe you are actually entertaining this troll... Look at his answers for crying out loud, he is just messing around with you guys, this post was created as flamebait in the first place. Either ban him, ignore him, sue him for spamming, lock his posts, or whatever, but answering his questions does not serve ANY purpose. The way you're behaving now makes everyone here look childish.

    Moderators, please do something about it.
    **sigh*

    I agree, and I am sorry. I should have followed Prelude from the start and closed the thread right after moving the off-topic posts from the other thread., instead of being curious of what "mathematical proof" esbo was talking about.

    You dont have to worry about the scope of the variable so you can use it anywhere in the
    program. With local cariables you have the extra concern of the scope thus global variables
    are easier to use.
    Sorry, that is not a mathematical proof.

    I note that esbo is wrong: with local variables there is no extra concern of scope, since the scope is entirely local.

    Which is more than I can say for, Windows XP, Internet explorer, Outloook express, Firefox.......etc.........etc...etc.......all extensible no doubt.
    I note that these programs are several magnitudes larger than what esbo has ever written, or will ever write.

    It is not a mistake the function does not return anything, where it that int it returned?
    I can't seem to find it.
    I note that esbo missed the point that without explicitly specifying a return value, the return value of a function is either int by default, or possibly undefined. To achieve what esbo wanted to achieve, use a void return type in your programs.

    It is perfectly easy to see where the variable is used in the program, you just do a search on it. The beauty of global variables is that they can be modified anywhere, it is not a failing it is a strength. Only takes a few clicks of the mouse to see where the variable is used.
    esbo clearly has never tried reasoning about the correctness of his programs (and yet dares to demand "mathematical proofs"). Searching for a mess of global variables makes reasoning about correctness difficult since you need to keep track of all the global variables of the program. With local variables, you only need to keep track of those variables in the current scope.

    Futhermore to say that local variables are easier to understand is wrong, local variables
    can change other variables in the program via return values. Not only that it could change
    any variable of the same type throughout the program.
    esbo does not understand the concept of flow of control: in fact, this is reason to believe that his "larger" programs are full of spaghetti code.

    Now if you want to find out how that local variable affects the rest of the program you have to look at all the calls to that function and various different variables affected by that call,
    esbo does not understand that one would not want to find out how a local variable affects the rest of the program. There is no need to, unlike for global variables. This is the very idea of locality of reference.

    I have seen stuff being passed down through 4 or 5 levels of functions, the name changing each time. In making their program 'less complex' and easy to follow they create the biggest labyrinth of complexity ever seen.
    Due to this global variable mindset, esbo misses the point that we do not need to care about the other functions, since we are only concerned with local scope. Within local scope, we choose the most descriptive names for our variables, thus documenting the program as it is written.

    If you want to make you program imcomprehensible avoid using global variables.
    Mind you when you come back to it in a few months you won't be able to undestand it either
    Computer science and engineering literature says otherwise, as does my own personal experience.

    What happens is a novice programmer discovers how to use local variables and then
    use then excessively because he now an 'experienced programmer'.
    Chance are you will never use the function again so you are just creating work for yourself
    and making your program unnecessarilly complicated. Every variable you pass is an oppertunity to make an error. get the types wrong and you will have some nice exotic bugs
    which could lay dormant for some time.
    esbo does not see another reason to use functions aside from extracting commonality in code: to help separate logical portions of the program.

    I have a 1000+ line program it has 17 local variables, about 80 global variables, and 6
    functions, one of which requires 2 arguements. None of the functions return any values.
    It works fine and is easy to follow.
    I don't find any problem whatsoever with the global variables. Infact it is the function which
    is passed values which is hardest to follow
    "Easy to follow" can only be the valid opinion of a code reviewer. The opinion of the code author means nothing, no more than that of a child proclaiming his first poem to be the greatest that was ever written.

    Now that I have addressed esbo's points (again going against Prelude's wisdom )...
    *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. COM data sharing
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 09-02-2007, 02:48 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM