Thread: Bad Habbit? (variable management)

  1. #1
    Maybe? Maybe not...
    Join Date
    Feb 2005
    Location
    Atlanta
    Posts
    16

    Bad Habbit? (variable management)

    Ive noticed over the last several programs Ive written for skill building that I have certain tendencies when managing data within a program.

    The biggest one that seems like a horrible habbit is my use of structures.

    for example if I have 5 objects that are all similar I would throw them all in a single structure and pass them back and forth throughout the program in order to keep track of everything and make it a little bit easier (in my mind at least) to write code to manipulate.

    I think it stems from only being able to return 1 value from a function. For some reason I don't want to declare anything as a global variable unless I absolutely have to for some reason.


    Code:
     
    struct SimilarData{
    	int group1;
    	int group1;
    	int group2;
    	int group2;
    	int group3;
    	int group3;
    }
    etc...

    and then pass the entire structure to any module that uses it and then pass it back.

    That really seems wrong though.

    Would it be better to make it a little bit harder to read and do it in individual structures

    Code:
     
    struct group1
    {
    	int group1;
    	int group1;
    }
     
    struct group2
    {
    	int group2;
    	int group2;
    }
    or just do them all as a single variable or in an array?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It really depends on what you need. A structure works fine if they're groups of mixed types. For items of the same type, an array works fine. Or perhaps an array of structures.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I would use an array with enums as indexes like this:

    Code:
    enum {
        enuArr1 = 0,
        enuArr2,
        enuArr3,
        enuArr4,
        enuArr5,
        enuLAST
    };
    
    struct DATA{
        int arr[enuLAST];
    };
    
    int main() {
        struct DATA dat;
        int i;
    
        for (i=0; i<enuLAST; i++)
            dat.arr[i] = 0;
    
        dat.arr[enuArr4] = 123;
    
        return 0;
    }
    In a little program like this it is silly, but crank this up to a 50,000 line program and all the sudden it becomes self-documenting.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your enumeration I don't like, because you are subscripting an array with enumerations that don't follow standard subscripting. (IE: You start at 1 where arrays start indexing at 0.) It doesn't follow standard convention, so it's a bit confusing at a glance to just see the line:
    Code:
    foo[ enumeration3 ] = 2;

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 03:13 AM
  2. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM