What is better practice between these two code snippets?

This is a discussion on What is better practice between these two code snippets? within the C++ Programming forums, part of the General Programming Boards category; What's better practice between these two pieces of code? Basically, do you want to return values as much as possible, ...

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    What is better practice between these two code snippets?

    What's better practice between these two pieces of code? Basically, do you want to return values as much as possible, or declare 'more global' variables and have the function change the variable.

    Option 1
    Code:
    int foo = 0;
    
    int main() {
       aFunction();
    }
    
    void aFunction() {
       foo = someValue;
    }
    Option 2
    Code:
    int main() {
       int foo = aFunction();
    }
    
    int aFunction() {
       return someValue;
    }
    edit: holy macorel! just seconds after posting this, this post appeared on a google search. Google will probably take over the world.
    Last edited by jrahhali; 11-25-2010 at 08:21 PM.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Are you for real?

  3. #3
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    yes, I am for real.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758

  5. #5
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    thanks whiteflags, that helped.

    So, from what I gathered, returning a value as often as you can is better than modifying a global variable because it keeps things 'neater', and, when a bug does occur, it's easier to track down.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    That's correct. If you really need something like a global variable, you could also use the singleton design pattern, which at least guarantees one instance of an object only, and that is, at times, important.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing Code Snippets
    By saeculum in forum Linux Programming
    Replies: 1
    Last Post: 03-15-2009, 09:47 PM
  2. Joining these code snippets
    By spadez in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 04:07 PM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Couple Code Snippets
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 01-22-2002, 02:23 AM

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