And, regarding "ugly"... ALL code is ugly. Yours, mine, everyone's. Code Is Ugly.
Just face it. When someone says "beautiful code", hear "beautiful rotten entrails".
The only beautiful code, the most clean code, is no code at all. If it's there, it's
ugly. The unfortunate fact of software development is that, in order to create
beautiful software, we must use ugly code.
The premise that 'all code is ugly' is never really substantiated.
I believe code that accurately and clearly portrays the algorithm is beautiful code. For example, quicksort in Scala is concise, and reads like the mathematical description of the algorthm:
While I see the problem with this implementation of QuickSort, I don't agree that "it's hard to find beautiful code in 'real world' problems." I think most programmers have opportunities to write elegant code every day.
I agree with this definition of beautiful. The title sounds like yet another opportunity for everyone to display how humble they are. If I heard someone say that at work, it would make me wonder if they're too lazy to try harder.
On the other hand, "make sure that there's as little code as absolutely necessary" is absolutely my prime directive as a programmer.
Just like the oft-quoted two-line Haskell definition is not really quicksort, this isn't either. It has to be in-place to avoid worst-case O(n^2) space.
I've noticed people often get bogged down on the issue of "beautiful" code in focusing on the syntax of the language. But that's only a tiny fraction of what makes code beautiful. The true beauty is in the underlying algorithm or idea that the code is implementing. Much like calculus problems aren't what I would call beautiful, but the underlying mathematical principles are.
Code is ugly or beautiful based on what you're used to seeing. This is the reason for the endless style debates: the way you're used to working just looks better to you.
Whenever I find a new way of working which has actual benefits, I just use it until it looks better to me.
In this case, it looks like there are real benefits to the comma-first method, so I plan on getting used to it.
P.S. By the way, the things you're talking about (clear, accurate algorithms) are not the things the author is talking about. He's talking about style, you're talking about substance. In your example, changing the style to C++ style brackets instead of K&R wouldn't make your point any less valid.
If you'd like trailing commas in your JavaScript, one of the tiny things that CoffeeScript does is allow trailing commas, or for commas to be omitted altogether on multi-line array and object literals.
This feature is also in C and in Go language. It makes especially easy to write code generators, because you don't have to deal with first/last special cases.
"If you disagree, then apparently, "clean" just means "looks like what I saw yesterday and the day before"."
Well
, sometimes the way it was done yesterday simply _is_ the right way to do it
. Of course
, one can occasionally try to challenge fundamental grammatical conventions like putting punctuation on the end of a sentence instead of the front of a line
. To me
, "clean" or "beautiful" code is both easy to read and understand and follows the conventions and best practices that have proven themselves over and over
.
Comma-first is beautiful! Comma-first is functional.
When we make lists, do we put the * at the end, or up front? Comma-first serves the same purpose. It delimits a new entry. "here is a new entry! here is another!"
Comma-first is column aligned with the least about of white space. It visually identifies the list as such, set apart from other code. Easy to parse, easy to skim, easy to elide, depending on your need.
If separators are meant to be put before each item, as in comma-first style, they shouldn't have been commas in the first place. Commas should be used just like they're used in natural written language. If I were to design a language that uses comma-first style, I'd replace commas with bullets:
But actually, there are other benefits: easier to add new lines to the list (just duplicate the previous one and change the value; no need to remember to add a comma at the end of the last item).
Also, someone mentioned another (small) benefit in the comments: when diff'ing files, using comma-first will only cause one line of difference, instead of two.
These are all small benefits, but since I can't think of any disadvantages to the comma-first style, I think I'm a new "convert".
I don't use JS, but in PHP you can add a comma after the last item, which I usually do, and therefore I don't have the problems that you encountered. Of course, I believe that people should do what works best for them, and if you find comma first easier to work with, then great!
Edit: And in response to your first comment about your IDE not supporting this: Is it not trivial to identify a syntax error in JS when you attempt to run it? Surely the intepreter (browser?) will notify you of the line that the syntax error occured on?
Yeah, I'm also used to adding a comma after the last item (in C, C++, Python).
One of the problems is that this is an error in Javascript, but only in IE. Which means code will work fine while you develop, but when it comes time to test, it will suddenly stop working.
The good thing about this way is that it's also very easy for humans to spot the error. Bottom line, catching the error while you code is always better than catching it after you compile. Also, this method has the advantage of working in all the languages I work in.
What a misleading title. This is about putting commas in array initializations before the next line or after the last line and argues that ALL code is ugly, so it does not matter if it looks akward.
I think this is just an example. If the author really tried to demonstrate that all code is ugly... Well, it would be a very long gist, don't you think?
In determining the best coding style, ease of spotting errors is one thing to consider. Another is ease of interpreting correct behavior. At a glance, comma-first is harder, but that may just be because I'm used to comma-last.
Sure code is ugly. So is paint when it's sitting in it's container. But when used by someone with skill, great works of art are created. Same goes for code. Out of context it's ugly, but what it creates is beautiful.
I've sometimes had my moments where I thought "no code could ever look good". If you read enough DailyWTF-like resources you'll gain a sense of pessimism about code.
My humble attempt to find some code that's pleasant to look at: http://lovelyco.de.
ALL code is ugly. Yours, mine, everyone's. Code Is Ugly.
I couldn't agree more. It's 2010 and we're using text files to describe rules. We're using text files to describe data.
My God. Everyone just stop what we're doing and look at your code. Look at it! It's worse than ugly; it's monstrously stupid. It's idiotic to a degree that beggars the mind. And here we are, in 2010, ignoring the elephant in the room: that code, the way we do it, is petulantly retarded.
Your comment is long on hyperbole, short on reasoning. What, specifically, is wrong with a textual representation? What alternatives do you suggest?
People have tried more sophisticated representations than text before. In my experience, they offer little benefit, while throwing away a rich ecosystem of tools that work with text. It seems to me the most promising advancements have been in tools that work intelligently on top of simple text files, rather than trying to replace them.
Yeah, it was a bit troll-y, I know. But sometimes the Diogenes in me comes out and really, we all need to be slapped in the face and asked, "This? Is it? This is what we've done?" Hardware doubles every eighteen months. If it was a stock it would blow the markets away. Software is a T-Bill. It barely covers inflation. Shame on us for "having tried more sophisticated representations than text before" and then giving up when it didn't immediately pan out.
Humans have been writing language as text for thousands of years. You're going to have a hard time changing it.
What would be worse is something like mathematical or musical notation, with non-trivial layout rules and endless ambiguities. Or a complex binary format that only a computer can decode. Text files are actually pretty damn perfect as a simple mapping from computer (raw data) to human (language).
Why in the world would we want to make raw data to language?
You use a spreadsheet for your budget. You use an accounting system at work. Yet you use a text file to describe the mission critical rules your business runs on.
This is the misconception we've run into: that we think we need to move programming languages to human languages, when human languages are bereft of concision and determinant qualities. We need to move in the opposite direction.
So let's assume we manage to tackle all the problems associated with non-textual formats (merging, compatibility, opacity, etc). What does this new way of programming do better?
I would say it the other way. When we can really free ourselves from text, we'll have made the main core changes that have been needed to make programming better.
Given that human ability to think with abstractions is likely connected to our ability to use language, I don't see why textual representation would be the fundamental problem in programming.
There are subproblems (e.g. state machines) in our craft that benefit enormously from different kind of (visual) representations, but in my opinion textual representations is the best basis for programming.
On the otherhand, you might just hate text files as a way to organize code and would instead use a more elaborate format. Maybe there is better ways, but I think simpler formats fare better when you need to communicate with other people and over organizational boundaries.
Regex? Really? Because that's exactly what I'm talking about. Regex is a quite simply the nadir of querying languages and challenges that we ever evolved up from monkeys.
Presumably "graphical" programming tools like labview and gedae. But I'm not a fan of them as they are very mouse-dominated which doesn't do my RSI any good.
I'm specifically not naming what I'm talking about because I didn't have it in 1996, when I started; I just had the anger and frustration. That's where you start.
You can't get a solution before you realize there's a problem.
I believe code that accurately and clearly portrays the algorithm is beautiful code. For example, quicksort in Scala is concise, and reads like the mathematical description of the algorthm: