Monday, January 04, 2010

Size does matter!

Its true. Maybe I should be more precise and say that code size matters.

Which is more readable of the following?



or


They both show the same code sample I nicked from the internet, and they both display the same number of lines.
The first one is the recomended style for C#. The second is more like Suns recommendations for Java.

The first sample uses a lot of lines for syntax. This means that you actually can't see the whole method without scrolling the window down. This is one of the reasons I prefer the second approach. The blank lines just bloats the code and makes it harder to view the code as a whole. Although I generally think formatting of code is of little importance to a project, there is no reason to have extra lines that contain no extra information.

5 comments:

  1. The first version is more readable due to the characters not sticking so close to each other.

    But both are pretty bad - ruby and python code is about 100x easier to read.

    ReplyDelete
  2. Hi shevegen and thanks for your comment. Isn't it true that neither ruby or python have brackets around blocks of code. Python uses indentation to group code and ruby uses the 'end' tag. In my opinion this means that they are closer to my second example, don't you?

    ReplyDelete
  3. It's the good old discussion that can be very religious at times. Using curly brackets after or before a newline " \n" is a matter of ethics and beauty.

    I.e.

    #include

    int main()
    {
    printf("Hej Verden!\n");
    return 0;
    }

    Is far more ethic correct in my POV than:

    #include
    int main() {
    printf("Hej Verden!\n");
    return 0; }

    Because I am no machine !

    BCPL was actually the first language to use curly brackets to outline multi-statement function bodies as one of many stylistic ways to format a program. STYLISTIC!
    Hence the refereal to ethic and beautifully written code. When it comes to practical use it’s a matter of working with STYLISTIC code. 1TBS indent style as your style is called, can be as useful as any other. But again is a matter of STYLISTIC code. Beauty is everything. Have you ever seen Einstein's equations? They are so beautiful in style (and content)
    By the way, I am more into Kernighan and Ritchie's style which was what I read in my first computer book The C Programming Language.

    The indent war has begun :)

    ReplyDelete
  4. Hi Jake, Looks like Kernighan has written an interesting book on the subject. I will have to read that at some point

    ReplyDelete