Thursday, May 24, 2012

Programming Style

The mark of a good computer programmer is one who A) has a coding style and B) is consistent in its use.  But programming styles are almost as unique as the developer using it.  This becomes a pain when you're working on a project with other developers, or when you're copying code from the Internet.  Anyway, the main purpose of this post is to talk about two main coding styles.

Style 1 - Old-School
I don't know the name (assuming there is one) for this first style, so I'm going to call it "old-school" (more on that later).  In this style variables and function names are all lowercase, and the underscore (_) is used in place of spaces.  A typical block of code might look like this.

int string_length(char *input_string)
{
    int string_length = 0;

    if(input_string != NULL && *input_string != '\0')
        string_length = calc_length(input_string);

    return string_length;
}


Style 2 - Hungarian Notation with CamelCase
The second common style is a combination of Hungarian notation with CamelCase.  This means variables are prefixed with the type of variable.  Also variables and functions have the first letter of each word capitalized and don't use spaces.  In this second style the above function would look like this:

int StringLength(char *szInput)
{
    int nStringLength = 0;

    if(szInput != NULL && *szInput != '\0')
        nStringLength = CalcLength(szInput);

    return nStringLength;
}



Me personally, I greatly prefer the second coding-style.  It's easier to read (although that's largely due to the fact that I've been coding in this style for a decade).  It also is easier to type.  This style tends to be less wordy, plus it has fewer underscores which isn't the most convenient key stroke combination to type over and over.  Finally, I think it makes for better coding and debugging.  It helps to look at a variable and at a glance know what type it is.  If I see a variable "customer_number" I don't know, is it a plain number or is it a string representation of a number - which makes a huge difference.  But if I see "strCustNum" or "nCustNum" I know which type it is.

Lastly I wanted to explain why I referred to style #1 as "old-school."  As far as I can tell, this coding style originated in the 1960s or 1970s.  It's been around a while, and I would argue has been superseded by better styles.  Another thing I've noticed, style #1 is primarily used by Unix developers whereas style #2 is primarily used by Windows developers.

1 comment:

  1. I agree that I hate when a single file has several different styles of programming. It's better to have one consistent style even if I don't like that particular style than to have multiple types of styles.

    The styles you describe are very much C/C++ programming styles. In Java I don't think I'd ever pre-prend the type to my variable name. The IDE handles much of the typing issues for me. My person style is similar to yours with the exception that I like the open brace on the same line as the expression and camelcase for the variables (minus the hungarian notion).

    ReplyDelete