About | ACP | Buy | Forum | Industry Watch | Learning Curve | Search | Twitter | Xnews
Home » Learning Curve

How to be Popular with Your Boss I

For programmers. Learning the 'right' way to code to get ahead. First in a series.


Dennis Ritchie, Alan Feuer, Ken Thompson, Brian Kernighan, Doug McIlroy - and tosomeextentSteveJohnson: these are good coders. Some of the best.

But they're researchers - they work in a lab or a university. They invent things. They can do things any way they please. If you're holding down a programming job, you can't - not if you want to get ahead.

Most programming today is based on C. C was one of the first context free parsing languages. Languages like C - and especially C itself - are extremely powerful and can be extremely terse. Maybe not like APL but almost.

These languages let you express things in a very natural and very concise way - but because they're context free parsing you can use them for the exact opposite. To your advantage.

A few examples might make this clearer.

Nested Expressions

Expressions can easily be nested in C. It makes code very easy to read. Unfortunately it also makes code more concise - making it look like you've done less work. The more lines of code you write, no matter what anybody says, the better your chances of advancement. The larger your source files, the more you look the programming genius. The following is well known.

int c;

while ((c = getchar()) != EOF)
    ;

You've seen it a million times in books and other people's code. It's bad - it's wrong.

int c;

c = getchar();

if (EOF == c) {
    ;
} else {
    ;
}

This is the preferred way to do things.

  1. You put EOF first in the evaluation. Your boss has read somewhere this is supposed to be better. You win points.
  2. You 'un-nest' the conditional expression. This makes it easier to understand for people like your boss. Your boss is not only far from the sharpest tool in the shed, he's also the guy with your promotion in his pocket. Stay on his wavelength.
  3. Topsy turvy logic. Loses points in comp sci 101 but your boss will love it.
  4. Excessive obsessive use of curly braces. Your boss loves curly braces.
  5. You used over twice as many lines of code.

The switch statement

Kernighan, Ritchie, and K/R will write switch statements a sensible way. There are risks associated with this behaviour.

int c;

while ((c = getchar()) != EOF)
    switch (c) {
    case 'a':
        do_a(); break;
    case 'b':
        do_b(); break;
    case 'c':
        do_c(); break;
    default:
        do_default(); break;
    }

This is a nice switch statement and is easy to read. Much too easy. Compare with the following.

int c;

c = getchar();

if (EOF == c)
    {
        ;
    }
else
    {
        switch (c)
            {
                case 'a':
                    do_a();
                    break;
                case 'b':
                    do_b();
                    break;
                case 'c':
                    do_c();
                    break;
                default:
                    do_default();
                    break;
            }
    }

This format is far superior. It wastes an obscene amount of white space. Also, it's 'fartsy' - it defies imagination and sensibility, and so it's a good bet your boss will think this is good code. Remember that for your boss the code does not have to be good - it just has to look like it's good.

Parentheses

First and last rule of parentheses: use 'em - as many as you can. Parentheses see the bindings occur in the right order - but if things are already in the right order they can hardly hurt, only make you look better. Consider the classic Celsius Fahrenheit formula.

int celsius_to_fahrenheit(int celsius) {
    return (9 * celsius) / 5 + 32;
}

Everyone knows the division binds before the addition but your boss won't know - and even if you tell him he'll forget. So make it clear what you mean.

int celsius_to_fahrenheit(int celsius) {
    return ((9 * celsius) / 5) + 32;
}

That takes care of the most obvious binding issue, but now applying the principles you've learned above you can turn this function into a work of art.

int celsius_to_fahrenheit(int celsius) {
    int fahrenheit = 0;

    fahrenheit = ((9) * (celsius));
    fahrenheit = (fahrenheit / (5));
    fahrenheit = (fahrenheit + (32));

    return fahrenheit;
}

If your boss asks why so many parentheses, tell him it's for protection. Note as well how we took a really thin candidate for inline and made it into something lugubrious. This is the essence of the type of programming that will get you ahead in your career.

And while you're at it, dispense with that dinky GNU style. Your boss is hardly going to be a proponent of open source anyway.

Documentation

Documentation is very important - not for you but for your boss. It not only shows him you've been working hard, it helps him understand what you've been doing.

/*
 * int
 * celsius_to_fahrenheit ( int celsius );
 *
 * This function takes a given input
 * celsius temperature value and return
 * the same temperature in fahrenheit.
 *
 * Both the input and output values are of
 * type 'integer'.
 *
 * The program works by first allocating a
 * storage area for the automatic variable
 * 'fahrenheit' and initialising it to 0.
 *
 * The program then multiplies the input
 * celsius value by 9 and assigns this value
 * to 'fahrenheit'. It then divides this value
 * by 5 and finally adds 32.
 *
 * The value of 'fahrenheit' is then returned.
 */

/* HISTORY
 *
 * 2007-01-01 SB First version
 * 2007-07-02 SB Minor bug fixes
 * 2007-10-02 SB Finish documentation
 */

///////////////////////////////////
// Main function begins shortly. //
///////////////////////////////////

/////////////////////////////////////////////
// ** BEGIN celsius_to_fahrenheit BEGIN ** //
/////////////////////////////////////////////

int
celsius_to_fahrenheit ( int celsius )
    {
        // Create fahrenheit and initialise to zero (0).
        int fahrenheit = 0;

        // Multiply celsius by 9 and
        // assign product to fahrenheit.
        fahrenheit = ( ( 9 ) * ( celsius ) );

        // Divide fahrenheit by 5.
        fahrenheit = ( ( fahrenheit ) / ( 5 ) );

        // Add 32.
        fahrenheit = ( ( fahrenheit ) + ( 32 ) );

        // This line intentionally blank.

        // Return result.
        return fahrenheit;
    }

/////////////////////////////////////////
// ** END celsius_to_fahrenheit END ** //
/////////////////////////////////////////

Look over the snippets in this section and ask yourself honestly: who gets the promotion?

About | Buy | Forum | Industry Watch | Learning Curve | Products | Search | Twitter | Xnews
Copyright © Rixstep. All rights reserved.