Thread: Globals?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    795

    Globals?

    Can anyone explain the relative benefits / drawbacks of using globals? From what I've read on google/other sources, they're not good practice.

    I can see why, but are the measures taken to avoid using them actually worse than just using them? For example: until now, all of my programs have been passing around large series of structs to all of its functions, containing data that it might not even need. Is this really wasteful, or more so than globals?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by memcpy
    I can see why, but are the measures taken to avoid using them actually worse than just using them? For example: until now, all of my programs have been passing around large series of structs to all of its functions, containing data that it might not even need. Is this really wasteful, or more so than globals?
    Why are you "passing around large series of structs to all of its functions, containing data that it might not even need"? This could be a sign that you are not writing functions to do one thing and do it well.
    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

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It would appear our friend has replaced a bad programming practice with a worse one...

    Like Lase says... a function should do one thing and do it well.

    You don't need to give it everything, including the kitchen sink... just give it what it needs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extern / Globals
    By Tonto in forum C++ Programming
    Replies: 10
    Last Post: 03-15-2009, 11:37 AM
  2. Globals initialization
    By New++ in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2004, 01:11 PM
  3. avoiding globals
    By joed in forum C Programming
    Replies: 10
    Last Post: 09-05-2004, 10:15 PM
  4. On globals of different modules.
    By BrownB in forum C Programming
    Replies: 1
    Last Post: 07-16-2004, 11:55 AM
  5. globals
    By ygfperson in forum C Programming
    Replies: 3
    Last Post: 08-19-2002, 02:49 PM