Linked by Thom Holwerda on Tue 15th Jan 2013 21:24 UTC
General Development "I was really excited to write this article, because it gave me an excuse to really think about what beautiful code is. I still don't think I know, and maybe it's entirely subjective. I do think the two biggest things, for me at least, are stylistic indenting and maximum const-ness. A lot of the stylistic choices are definitely my personal preferences, and I'm sure other programmers will have different opinions. I think the choice of what style to use is up to whoever has to read and write the code, but I certainly think it's something worth thinking about. I would suggest everyone look at the Doom 3 source code because I think it exemplifies beautiful code, as a complete package: from system design down to how to tab space the characters." John Carmack himself replies in the comments.
Thread beginning with comment 549005
To view parent comment, click here.
To read all comments associated with this story, please click here.
RE[4]: Good article
by Laurence on Wed 16th Jan 2013 13:53 UTC in reply to "RE[3]: Good article"
Laurence
Member since:
2007-03-26


Compare that to the C#...


That's badly formatted C#

I prefer the following:
if (X == Null) {
    Something();
    Somethingelse();
} else if (Blah == "Give up") {
    Somemore();
} else {
    ShootYourself(DateTime.Now);
}


In my opinion that is significantly more readable than any of the other examples you've listed.

Sometimes I concatenate the lines further:
if (X == Null) {
    Something();
    Somethingelse();
}
else if (Blah == "Give up") { Somemore(); }
else { ShootYourself(DateTime.Now); }

(this isn't the best of examples (and not helped by the formatting on OSNews), even I wouldn't concatenate specifically here, but sometimes it does aid readability.

edit:
The beauty Pascal is that the entire If is one statement with compounds, the beauty of the C# is that it is not (really.) Both have advantages.

You can use blocks in C-derived languages as well:

{
    if (X == Null) {
        Something();
        Somethingelse();
    } else if (Blah == "Give up") {
        Somemore();
    } else {
        ShootYourself(DateTime .Now);
    }
}


Edited 2013-01-16 14:10 UTC

Reply Parent Score: 2

RE[5]: Good article
by lucas_maximus on Wed 16th Jan 2013 14:01 in reply to "RE[4]: Good article"
lucas_maximus Member since:
2009-08-18

Actually apart from the lack of indentation it was correct. The Visual Studio default and the Microsoft Recommended Coding conventions all use that style.

http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx

Open braces should always be at the beginning of the line after the statement that begins the block. Contents of the brace should be indented by 4 spaces.

Reply Parent Score: 1

RE[6]: Good article
by Laurence on Wed 16th Jan 2013 14:19 in reply to "RE[5]: Good article"
Laurence Member since:
2007-03-26

Yeah I'm aware of that and I don't agree with it. I think it's less readable and the opposite of what developers in most other C-derived languages prefer.

Plus I object to Microsoft dictating how I -or any other 3rd party developer- chooses to format the braces in our own source code. That's my prerogative, not theirs.

Edited 2013-01-16 14:21 UTC

Reply Parent Score: 2

RE[6]: Good article
by Nelson on Wed 16th Jan 2013 16:01 in reply to "RE[5]: Good article"
Nelson Member since:
2005-11-29

I usually put braces on a new line unless the block is one line long.

if (someArg == null)
throw new ArgumentNullException("someArg");

The only exception I usually have to this is if its part of a larger if-the-else statement and the other blocks are multiline and have curly braces. Then I think:

if (this == that)
doX();
else if (x == y)
{
// ..
}
else
{
// ..
}

I think the obsession with vertical space is kind of silly in this day and age. I am much more sensitive to code that sprawls horizontally forever.

Besides, I often find new line braces more readable while less compact, whereas the opposite is true for a lack of braces, same line braces, or first brace same line.

At the end of the day though, it is extremely annoying when some full-of-himself programmer ignores the established project coding guidelines. I don't care if a monkey wrote it, if I'm going to be a part of a team, I'm going to follow their rules.

Reply Parent Score: 2

RE[5]: Good article
by henderson101 on Wed 16th Jan 2013 16:14 in reply to "RE[4]: Good article"
henderson101 Member since:
2006-05-30

"
Compare that to the C#...


That's badly formatted C#
"

Your opinion, and bear in mind, OSNews lost my indentation. To me, your code looks suboptimal for readability. I work in a large team, other people picking up classes and libraries I've written is essential. Standard style guides and conformity saves a lot of time and prevents prima donnas. Plus, having come from Pascal, those arguments just waste too much time. Suffice to say:

Lazy programmeritis:

if (x = y) then begin
   .....
end else begin
   ....
end;

I once had a co worker that used this one:

if (x = y) then
  begin
    ...
  end
  else
    begin
    end;

And this one:

if (x = y) then
  begin
  .....
  end
else
  begin
  ......
  end;

Borland style:

if (x=y) then
begin
  ...
end;



And that is just scratching the surface and not even dealing with tabs, etc. It's a holy war, not worth winging about.

For C#, the basic layout I use (and enforce here) is based on the standard Microsoft use in all of their sample code. Sorry if you don't like that, complain to someone who has write access to Microsoft's source repository ;-) Visual Studio also usefully formats this way - why fight the IDE?



if ( X == Null )
{
  Something();
  Somethingelse();
}
else if ( Blah == "Give up" )
{
  Somemore();
}
else
{
  ShootYourself( DateTime.Now );
}



I never use tabs, they are evil. I use 2 spaces for "Tabstop" because otherwise your code shoots off to the right at great speed. I would go for simple over terse single line every time. I tend to (recently) add in extra space in if/else if/method params. This is just a concession to a speed reading on a high res monitor. Text scans better.

You can use blocks in C-derived languages as well


Not exactly the same thing. In Pascal this is one statement:

if (x = y) then dosomthing else dosomthingelse(y);

and so is this:

if (x = y) then begin dosomthing; end else dosomthingelse(y);

and so is this:

if (x = y) then begin dosomthing; doanotherthing(x); end else begin dosomthingelse(y); end;


dosomething and dosomethingelse(..) are compound, as are the other examples. Like I said, the C syntax is sort of similar, but not exactly. Pascal thinks of the compounds as being part of a single statement, with compound elements. Where as C doesn't (IIRC.) The braces in C are just a way to create a block and the if/else links those blocks together. Semantics, I know. But look again at the basic Pascal version:

if (x = y) then dosomthing else dosomthingelse(y);

That is a pure statement, with no semicolon after the first clause. Geddit? :-) C would require 2 statements linking the blocks:

if (x==y) dosomething(); else dosomthingelse(y);

Reply Parent Score: 2

RE[6]: Good article
by Savior on Sat 19th Jan 2013 15:27 in reply to "RE[5]: Good article"
Savior Member since:
2006-09-02


if (x = y) then dosomthing else dosomthingelse(y);

That is a pure statement, with no semicolon after the first clause. Geddit?


I don't. How is that any different from
(x == y) ? dosomething() : dosomethingelse(y);
?

Maybe it looks a bit more cryptic than its begin/end counterpart if you embed these kind of statements into each other, but not by much. Stacking ternary statements is always ugly and unreadable, regardless of the language used.

Reply Parent Score: 2