Union vs. Mask

This is a discussion on Union vs. Mask within the C Programming forums, part of the General Programming Boards category; Better: (defn) More readable, less likey for errors, etc, etc, etc. I have a chance to rewrite some C code ...

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    Union vs. Mask

    Better: (defn) More readable, less likey for errors, etc, etc, etc.

    I have a chance to rewrite some C code that was written before there was a standard. Many #defines are used where there should be enums and lots of unions are defined but never used.

    As long as I'm rewriting the code (and porting it from DOS to Linux) I'm seriously considering moving the unions to masked fields, then have a couple of functions, like setbit() and clearbit(), that I would define to accept a void*, int, int for the data, type of the data (from an enum), and which bit we should set/clear. I would then just use enums, bit-shifts, and all the bit-wise opps for setting/clearing the data.

    OR:

    I can just leave these as struct defns with unions and not worry about it. . .

    Which way would be "better"?

    EDIT: bit-shifts got ...... out???? Must have had a type-o.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Which way would be "better"?
    If it works, don't break it?
    My best code is written with the delete key.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    That's funny:
    Quote Originally Posted by My Boss
    Well it [the code] has worked for 20 years, so it must not be too bad
    Quote Originally Posted by me
    Then why did we change it?
    Quote Originally Posted by My Boss
    Hmmph.
    Seriously, though. . . you'd have to see the code to understand why I want to change it. The original programmer didn't 1) follow any convention -- there was no standard. 2) document 3) use descriptive names 4) not use hard coded values after assigning the same values to variables. . .

    I could continue, but I think you get the point.

    EDIT: For example:
    Quote Originally Posted by header file
    Code:
    typedef struct GPD {    /* GPDBITS - output record for gas plasma display */
      unsigned home : 1,    /* Output port 0 - GPD control */
           cursoron : 1,    /* Prolog 7601A TTL I/O card */
              clear : 1,
              write : 1,
              spare : 4,
               data : 8;    /* Output port 1 - GPD data */
    } GPDBITS;
    Quote Originally Posted by program file
    Code:
    void clearhome(void)
    {
      int k;
      int gpdstat;
    
      do {                            /* Wait until gpd not busy */
        gpdstat = inp(GPDSTAT);
      } while (gpdstat & 0x0008);
      (void)outp(GPDCNTRL, 0xff);
      (void)outp(GPDCNTRL, 0xfb);
      for (k=0; k <25000; ++k)        /* Changed k from 2500  18 Jan 03  PSC */
        continue; 
      (void)outp(GPDCNTRL, 0xff);
    }
    Last edited by Kennedy; 11-17-2006 at 12:50 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I understand how you feel. However, legacy code is a tricky business because it's often written like a high school book report and has been running critical systems for multiple decades. More often than not, an innocent change will hose everything and you'll spend days trying to figure out why. Repeat ad infinitum and that's what updating legacy code is like. I can't in good conscience advise you to muck about in working code just to make it look prettier, but since you're going to do it anyway (and I probably would too), make no assumptions, okay?

    Best to leave the bitfields for now though. Clean up the code and see if it works for you, then consider gutting the bit stuff.
    My best code is written with the delete key.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    an innocent change will hose everything and you'll spend days trying to figure out why
    Even worse when the small change seems to be OK... Till it goes to the customer... And only then something stops working... that seems to be in no connection with changes done...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by vart
    Even worse when the small change seems to be OK... Till it goes to the customer... And only then something stops working... that seems to be in no connection with changes done...
    Had that problem already. . . Most of it was due to not using the #defines. . . I foolishly changed the #define to move a menu item down one in the list. Something didn't work right, though, and I had lines that had garbage on it. I had to look through the whole thing every time I made even the smallest change.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Does it have any bug reports against it?
    What is the number of bugs per 1000 lines of code?
    If it's >1 bug / 1000 lines, then perhaps you're in rewrite territory.

    How many features are you expecting (or would like) to add?
    If it's more than 20% of the existing feature count, then perhaps you're in rewrite territory.

    As you've already experienced, even minor changes cause unforseen side effects. The worst of it is, this situation can only get worse as time goes on, as people who follow you have to guess at the things you were thinking of at the time. It will eventually reach the point where no further editing becomes possible as each bug removed adds one more bug somewhere else.

    > and porting it from DOS to Linux
    Expect lots of benign memory abuse on DOS (totally devoid of any memory protection) to bite you really hard when you move to Linux.
    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.

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Salem
    As you've already experienced, even minor changes cause unforseen side effects. The worst of it is, this situation can only get worse as time goes on, as people who follow you have to guess at the things you were thinking of at the time. It will eventually reach the point where no further editing becomes possible as each bug removed adds one more bug somewhere else.
    Eh hum. ::Clears throught to tell big fat lie:: This system will be end of life in 10 years. I will be the last person to code for this system.

    This is a G'ment system, so it cannot have changes without major in-depth testing. Also, as you see in the comment PSC was the last ones to make a change and they didn't do much. In fact, that was the reason for that post.

    As far as the memory errors go -- they didn't use any dynamic memory through the whole project. Just a bunch of static vars in functions. I don't even recall too many pointers, other than ref pointers in functions.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >This system will be end of life in 10 years. I will be the last person to code for this system.
    Yea, we've all heard that one before.
    My best code is written with the delete key.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > This is a G'ment system, so it cannot have changes without major in-depth testing.
    You said you were rewriting the code, or did you just mean "rewriting" ?

    Hell, if it's that close to end of life, why are you even bothering with a Linux port?
    The only plausable reason is to extend it's life.

    I think that's enough free consultancy...
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  5. Sorting a Union
    By andy in forum C Programming
    Replies: 4
    Last Post: 11-21-2001, 09:12 AM

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