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 548925
To read all comments associated with this story, please click here.
Good article
by WorknMan on Tue 15th Jan 2013 22:33 UTC
WorknMan
Member since:
2005-11-13

This is going to be a post about personal preferences ...

Being the novice programmer that I am, some of it was over my head, but other parts of it I could comprehend.

One thing that struck me is the part about where to put the braces. Personally, braces just look messy to me, so I'd rather use a language without them. IMHO, having a terminating statement like 'END IF' is a lot more elegant than having braces all over the f**king place.

And don't even get me started on semi-colons ;)

Reply Score: 4

RE: Good article
by ebasconp on Tue 15th Jan 2013 23:32 in reply to "Good article"
ebasconp Member since:
2006-05-09

You'll learn to love them; like the most of us ;)

You can start flamewars just because of mentioning where the curly braces should be!!

I actually prefer writing:

int main()
{
}


to

int main() {
}

but, apart of feeling it more natural and making easier to see where a block starts and ends, I do not have any excuse to prefer it over the other one.

Reply Parent Score: 4

RE[2]: Good article
by hashnet on Wed 16th Jan 2013 03:38 in reply to "RE: Good article"
hashnet Member since:
2005-11-15

Not just a matter of aesthetics; it also depends on the tools you use.

With XEmacs, when the cursor is at a closing brace, the line with the matching opening brace is shown in the status bar.
With this setup, it is advantageous to place the opening brace and function definition on the same line for quick reference.

Reply Parent Score: 2

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

I also think it depends on the language you are writing in where you should put it.

e.g.

If it is JavaScript Douglas Crockford recommends the opening brace should be on the first line, because of JavaScript semi-colon insertion mechanism.

Java is the same due to convention. Microsoft recommend you use opening brace on the following line as you do when writing C# and is the visual studio intellisense default.

Personally I somewhat agree with the OP and I have had a gutful of C style syntax.

Reply Parent Score: 2

RE[2]: Good article
by Wafflez on Wed 16th Jan 2013 10:58 in reply to "RE: Good article"
Wafflez Member since:
2011-06-26

Same, Allman style is the best!

Especially when programmers tend to have atleast 1200 pixels of height on their screens (well atleast I hope programmers are smarter than average consumer and don't give in into 16:9 fad just because it's cheaper for Samsung to make 23" 16:9 than 23" 16:10...).

Edited 2013-01-16 11:04 UTC

Reply Parent Score: 1

RE[2]: Good article
by Kochise on Wed 16th Jan 2013 16:40 in reply to "RE: Good article"
Kochise Member since:
2006-03-03

Please, be my guest :

http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid...

Especially check my CSkinProgress and WaterMarker source code : space instead of tab, 2 space indent, hungarian notation, one instruction per line, etc...

Kochise

Reply Parent Score: 2

RE: Good article
by panzi on Tue 15th Jan 2013 23:47 in reply to "Good article"
panzi Member since:
2006-01-22

I think curly braces stand out more from the rest of the code than yet another word.

Reply Parent Score: 2

RE: Good article
by ssokolow on Tue 15th Jan 2013 23:57 in reply to "Good article"
ssokolow Member since:
2010-01-21


One thing that struck me is the part about where to put the braces. Personally, braces just look messy to me, so I'd rather use a language without them. IMHO, having a terminating statement like 'END IF' is a lot more elegant than having braces all over the f**king place.

And don't even get me started on semi-colons ;)


I'll agree with you on the semicolons and I do like Python's indent-based block syntax, but using words rather than curly braces is one of the reasons I've never tried to learn Ruby and refer to it as the bastard child of Java, Perl, and BASIC.

(Though by no means the only reason. The standard library and core language syntax are full of caveats, the Unicode support is inferior to Python's, it's not used much outside of Rails and apps based on it, etc.)

I think curly braces stand out more from the rest of the code than yet another word.


Agreed. The only thing that stands out more is probably the indenting itself.

You'll learn to love them; like the most of us ;)

You can start flamewars just because of mentioning where the curly braces should be!!

I actually prefer writing:

int main()
{
}


to

int main() {
}

but, apart of feeling it more natural and making easier to see where a block starts and ends, I do not have any excuse to prefer it over the other one.


Seeing all that wasted vertical space just drives me nuts. (Partly because it means more scrolling, more time spent repositioning my Vim cursor when scrolling drags it along for the ride, and more time reacquiring my train of thought when scrolling sometimes disrupts it)

...not to mention it doesn't fit as nicely with my Python-originated mental model that an indent level (syntactic block) is generally associated with the first line prior.

In fact, I'm sure that, in some places, I've been guilty of writing slightly less than ideal code using things like the ternary operator (PHP, mostly) and list comprehensions (Python) in order to vertically-compact my code.

Edited 2013-01-15 23:59 UTC

Reply Parent Score: 4

RE[2]: Good article
by kwan_e on Wed 16th Jan 2013 00:07 in reply to "RE: Good article"
kwan_e Member since:
2007-02-18

In fact, I'm sure that, in some places, I've been guilty of writing slightly less than ideal code using things like the ternary operator (PHP, mostly) and list comprehensions (Python) in order to vertically-compact my code.


I personally think list comprehensions are a cool feature and should be used more, not just in Python. It may be harder to figure out what it does, but at a glance, at least you can tell what kind of things are in the list.

Reply Parent Score: 2

RE[2]: Good article
by galvanash on Wed 16th Jan 2013 00:43 in reply to "RE: Good article"
galvanash Member since:
2006-01-25

Yeah... The beauty of indent-controlled blocking in languages like Python and Coffeescript is that it kills multiple birds with one stone:

1. It eliminates a lot of noise.
2. It makes something that everyone does anyway for readability actually mean something.
3. It effectively kills most styling arguments (not all, but most).

Anyway, I'm quite fond of it.

Reply Parent Score: 2

RE[2]: Good article
by moondevil on Wed 16th Jan 2013 10:04 in reply to "RE: Good article"
moondevil Member since:
2005-07-08

And then the nicely formatted code gets thrown away when another team member gets to touch the code.

That is why I just follow what is the project guidelines, regardless how I feel about them.

Reply Parent Score: 3

RE: Good article
by kwan_e on Wed 16th Jan 2013 00:02 in reply to "Good article"
kwan_e Member since:
2007-02-18

IMHO, having a terminating statement like 'END IF' is a lot more elegant than having braces all over the f**king place.


Well, at least it's your "humble opinion"...

Reply Parent Score: 6

RE: Good article
by Laurence on Wed 16th Jan 2013 00:02 in reply to "Good article"
Laurence Member since:
2007-03-26

Having programmed in both, I they both have pros and cons.

The real problems are:
1: badly formatted code,
2: lack of naming conventions,
3: and bad program design

1
--
Badly formatted code will make even the most readable of languages a struggle to follow.

2
--
It doesn't matter how well you format your code, if your function / variable names are badly chosen (eg abbreviated names that mean nothing to anyone but the developer) and there's no clear naming convention, then it can be a tough job understanding what the routine does.

I will also add, overly verbose names are bad too (I have one colleague who uses entire sentences in their function names. While it's helps explain what the function does, it makes code readability more challenging).

3
--
Bad program design is a more subjective topic. Most (if not all) developers roughly agree on indentation; and naming conventions only have to be readable, concise and consistent. Not everyone agrees on the specifics of naming conventions, but as long as those 3 criteria are met, then anyone new to the code can quickly understand the standards applied to that particular project.

However bad program design is about the code design of the code - when to you objects, functions, iteration and so on. Sometimes application performance is paramount, which often means writing clever routines in a manner that isn't ideal for a human reading back. Other times you'll be writing a program which isn't going to tax the processor much, so you can afford to opt for readability over performance (Sometimes you're lucky and the optimised code is also the most readable code).

Then you have people using obscure language features or unpopular features (goto is a great example of that. But thankfully there's very few occasions when a goto is the better method of writing a routine).

And after all that, you have the topic of code separation (eg using MVC's in web development to keep server side code separate from HTML).

----------

At the end of the day, it doesn't really matter whether your language uses English words or braces - a good developer can make their code readable.

But also, a lot of it comes down to practice. If you've never spent any time developing in C-derived languages, then the C syntax will naturally look a mess. But most developers get used to it and even come to love braces (that certainly happened to me).

Reply Parent Score: 3

RE[2]: Good article
by ebasconp on Wed 16th Jan 2013 01:25 in reply to "RE: Good article"
ebasconp Member since:
2006-05-09

I had a computer programming teacher that used to prefer:

a = a + 1;

to

a++;

in the name of readibility!!!

So, "readable code" is a very subjective term and in this case, if my teacher could not read the "++" thing... we have a problem!

Reply Parent Score: 2

RE: Good article
by henderson101 on Wed 16th Jan 2013 10:39 in reply to "Good article"
henderson101 Member since:
2006-05-30

IMHO, having a terminating statement like 'END IF' is a lot more elegant than having braces all over the f**king place.


No, no it's not. You try to maintain someone else's VB.Net app... "End If"'s (and "End ..." blocks in general) are complete crud. Braces are only complicated if you don't line them up correctly.. but most modern IDE's don't even make that hard. I far prefer '}' to working out which flavour of 'End ...' is missing in a VB method that some idiot made span 3 or 4 screens. Dometimes life is too short to not refactor, and moving VB code to C# is not all that hard. In fact, the last time I did that I fixed about 3 or 4 obscure bugs that VB introduced due to it's utter shit syntax (and this is with Option Explicit and Option Strict on.)

Having come from a Pascal background (but having done a lot of C++ in my BeOS days, ADA at university and BASIC before that), the "begin .. end" block teaches you much better practices than C's braces. There are plenty of places I will use a block where other programmers will not, mainly because it saves time in the long run.

And don't even get me started on semi-colons ;)


The semi-colon is a fairly unambiguous end of statement marker. If that is hard to understand, you are pretty much doomed.

Reply Parent Score: 2

RE[2]: Good article
by Treza on Wed 16th Jan 2013 12:09 in reply to "RE: Good article"
Treza Member since:
2006-01-11

I use both punctuation (C) and verbose (ADA) languages and tolerate both well.

The solution is a syntax colouring editor with very different colours for keywords and plain identifiers (not black and dark blue...)

The begin/end stuff becomes colorful patches surrounding black code.

Reply Parent Score: 1

RE[2]: Good article
by lucas_maximus on Wed 16th Jan 2013 13:15 in reply to "RE: Good article"
lucas_maximus Member since:
2009-08-18

Normally most formatting problems can be sorted with the IDE.

TBH a lot of the problems you describe are more to do with the way the applications have been written and not the language itself. I am sure I can point you to some of the atrocities I have seen in C# (my favourite being the INullObject interface I recently found).

Personally I like VB.NET, while the syntax is odd you can write code pretty quickly. My main issue with the language is that it is difficult to see the difference at a glace between an array and a procedure/function.

A lot of newer developers especially those coming from Python or Ruby find the use of a semi-colon slightly arduous. There was recently a flame war between Douglas Crockford and one of Twitter Bootstrap developer over whether you should make of the JavaScript semi-colon insertion mechanism.

Edited 2013-01-16 13:16 UTC

Reply Parent Score: 3