Critique my code?

This is a discussion on Critique my code? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Mastadex The reason I say this is that I avoid using global variables because there are ways ...

  1. #16
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Quote Originally Posted by Mastadex
    The reason I say this is that I avoid using global variables because there are ways of storing things that make golbal variables obsolete.
    I disagree.

    Quote Originally Posted by Mastadex
    for example, if you use malloc/new to dynamically allocate memory, and then pass around a pointer, that would give you the same result, but agian its quite limited.
    True, it could give you the same result, but that is not always the issue at hand. Did you know that every time you create a function that takes in more than 3 parameters, the function call is slower and takes more time, but if there are 3 or less parameters, it can be highly optimized? Often times in game development, there are SEVERAL variables from all across the game that need to be used in one function, and passing them in all in with pointers just simply isnt logical, reasonable, or efficient. Globals do the job.

    Quote Originally Posted by Mastadex
    If your going to call a function over and over agian and you need to keep a variable form going out of scope, you can always try making a variable inside the function static.
    True, that can be done as long as the variable is needed in that scope only.

    Now, I am not trying to say: Use globals!! Globals rock!!! Use them freely!!!

    I would never say such a thing as that, because it would be stupid to do such a thing, but I think advanced programmers should know that globals are okay sometimes, and not to be afraid of using them. They are not bad things at all. Beginners should not use them just because they need to learn good programming practice.
    My Website

    "Circular logic is good because it is."

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >True, that can be done as long as the variable is needed in that scope only.
    And you don't care about the function being re-entrant.
    My best code is written with the delete key.

  3. #18
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    How do you mean using global/local static variables decide whether a function is reentrant or not?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How do you mean using global/local static variables decide whether a function is reentrant or not?
    I mean using local static variables often makes a function non-reentrant because they're most often used to save state between calls, like strtok.
    My best code is written with the delete key.

  5. #20
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I agree, I thought you meant 'as opposed to global variables' because that was the context.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #21
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > Did you know that every time you create a function that takes in more than 3 parameters,
    A nice x86-centric view of the world there.
    The minute cost of a function call is a poor excuse for making everything global.

    > Often times in game development,...
    If they're that common, why aren't they all in the same structure which can be passed as a single pointer?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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