Branch

Sally wants more privacy. She decides to create her own named branch.

lottery sally$ vv branch new no_boys_allowed
Working copy attached to no_boys_allowed. 
A new head will be created with the next commit.

Now that Sally is working in her own branch, she feels much more productive. She adds support for the “favorite” option. When a user is playing his favorite numbers, his chances of winning should be doubled. In doing this, she had to rework the way command-line args are parsed. And she removes an atoi() call she missed last time. And she restructures all the error checking into one place.

So main() now looks like this:

int main(int argc, char** argv)
{
    int balls[6];
    int count_balls = 0;
    int favorite = 0;

    for (int i=1; i<argc; i++)
    {
        const char* arg = argv[i];

        if ('-' == arg[0])
        {
            if (0 == strcmp(arg, "-favorite"))
            {
                favorite = 1;
            }
            else
            {
                goto usage_error;
            }
        }
        else
        {
            char* endptr = NULL;
            long val = strtol(arg, &endptr, 10);
            if (*endptr)
            {
                goto usage_error;
            }
            balls[count_balls++] = (int) val;
        }
    }

    if (6 != count_balls)
    {
        goto usage_error;
    }

    int power_ball = balls[5];

    int result = calculate_result(balls, power_ball);

    if (result < 0)
    {
        goto usage_error;
    }

    if (LUCKY_NUMBER == power_ball)
    {
        result = result * 2;
    }

    if (favorite)
    {
        result = result * 2;
    }

    printf("%d percent chance of winning\n", result);

    return 0;

usage_error:
    fprintf(stderr, "Usage: %s [-favorite] (5 white balls) power_ball\n", argv[0]);
    return -1;
}

I despise if statements without braces. Reviewers of the early drafts of this book observed that I could save 14 lines and fit the previous code listing on a single page if I compromised my principles. I refused.

She commits her changes, knowing that the commit will succeed because she is working in her private branch.

lottery sally$ vv commit -m "add -favorite and cleanup some other stuff"

    revision:  18:37939b07309af8232c44048ca0a1633c982b7506
      branch:  no_boys_allowed
         who:  sally
        when:  2011/05/31 11:41:37.432 -0500
     comment:  add -favorite and cleanup some other stuff
      parent:  17:d934a35fc8eda4fec7cb6b0d049e3881cd0e4a1d

lottery sally$ vv push
Pushing to http://server.futilisoft.com:8080/repos/lottery:
Pushing... Done.