1 Replies - 2007 Views - Last Post: 17 August 2018 - 01:08 PM

#1 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6817
  • View blog
  • Posts: 23,191
  • Joined: 05-May 12

Challenge: Getting Duff's Device through a code review

Posted 04 July 2018 - 05:34 AM

We've not had a challenge posted in a very long time. Instead of a fun coding challenge, here's more of a interesting meta-coding challenge. Writing code is often a team effort, and that means writing code that complies with your teams' coding standards.

If for some reason or another, you were forced to implement Duff's Device, how would you format it to get it through a code review if your team uses OTBS? Allman?

For reference here's the original C for Duff's Device:
send(to, from, count)
register short *to, *from;
register count;
{
    register n = (count + 7) / 8;
    switch (count % 8) {
    case 0: do { *to = *from++;
    case 7:      *to = *from++;
    case 6:      *to = *from++;
    case 5:      *to = *from++;
    case 4:      *to = *from++;
    case 3:      *to = *from++;
    case 2:      *to = *from++;
    case 1:      *to = *from++;
            } while (--n > 0);
    }
}



Since my team uses Allman for any language that does not start with "Java" here's what mine would look like. We also require explicit "fall through" comments. It's as ugly as sin, and breaks our 20 lines per function limit:
void send(char *to, char *from, int count)
{
    int n = (count + 7) / 8;
    switch (count % 8) 
    {
    case 0:
        do 
        {
            *to = *from++;
            // *** FALL THROUGH ***
    case 7:
            *to = *from++;
            // *** FALL THROUGH ***
    case 6:
            *to = *from++;
            // *** FALL THROUGH ***
    case 5:
            *to = *from++;
            // *** FALL THROUGH ***
    case 4:
            *to = *from++;
            // *** FALL THROUGH ***
    case 3:
            *to = *from++;
            // *** FALL THROUGH ***
    case 2:
            *to = *from++;
            // *** FALL THROUGH ***
    case 1:
            *to = *from++;
        } while (--n > 0);
        break;
    }
}



Although, I would also present this alternative for them to look at to see if they are willing to suspend all the other team styling rules just to show how much more legible the code could be:
send(char *to, char *from, int count)
{
    // WARNING: ALL SWITCH CASES BELOW INTENTIONALLY FALL THROUGH!!!

    int n = (count + 7) / 8;
    switch (count % 8) 
    {
    case 0:     do
                {
                    *to = *from++;
    case 7:         *to = *from++;
    case 6:         *to = *from++;
    case 5:         *to = *from++;
    case 4:         *to = *from++;
    case 3:         *to = *from++;
    case 2:         *to = *from++;
    case 1:         *to = *from++;
                } while (--n > 0);
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Challenge: Getting Duff's Device through a code review

#2 german-one   User is offline

  • New D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 40
  • Joined: 05-August 18

Re: Challenge: Getting Duff's Device through a code review

Posted 17 August 2018 - 01:08 PM

No answers yet? I wouldn't interpret this as nobody cares how to use braces or how to indent code. I found my own style (I'm a hobbyist) which is derived from K&R (I omit braces for a single line) and Almann, but I think for the majority of programmers it's rather an agreement of a team as you already mentioned.

My idea for 1TBS ...
void send(char *to, char *from, int count) {
    int n = (count + 7) / 8;
    switch (count % 8) {
        case 0:
            do { 
                *to = *from++;
                case 7:
                    *to = *from++;
                case 6:
                    *to = *from++;
                case 5:
                    *to = *from++;
                case 4:
                    *to = *from++;
                case 3:
                    *to = *from++;
                case 2:
                    *to = *from++;
                case 1:
                    *to = *from++;
            } while (--n > 0);
    }
}


... and Almann ...
void send(char *to, char *from, int count)
{
    int n = (count + 7) / 8;
    switch (count % 8) 
    {
        case 0:
            do
            { 
                *to = *from++;
                case 7:
                    *to = *from++;
                case 6:
                    *to = *from++;
                case 5:
                    *to = *from++;
                case 4:
                    *to = *from++;
                case 3:
                    *to = *from++;
                case 2:
                    *to = *from++;
                case 1:
                    *to = *from++;
            } while (--n > 0);
    }
}



As you can see I also like the additional indentation of the case statements.

Steffen
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1