

ALL code is ugly - old_sound
http://gist.github.com/357981

======
mhansen

      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:

    
    
      def sort(xs: Array[Int]): Array[Int] = {
        if (xs.length <= 1) xs
        else {
          val pivot = xs(xs.length / 2)
          Array.concat(
            sort(xs filter (pivot >)),
                 xs filter (pivot ==),
            sort(xs filter (pivot <)))
        }
      }

~~~
_pius
... which is butt-ugly compared to QuickSort in Haskell:

    
    
      qsort [] = []
      qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)

~~~
ramchip
Yes, and now try this:

    
    
        import Debug.Trace
    
        qsort [] = []
        qsort (x:xs) = trace "(Called)" qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)
    
        Prelude> qsort $! [1..100]
    

Problem is, both of these "cute implementations" would be unusable in
practice. It's hard to find beautiful code in 'real world' problems.

~~~
_pius
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.

------
fragmede
As contrived as those examples are, Python does have one feature that I
currently miss, which is that trailing commas are ignored.

    
    
      mylist = [
        "ape",
        "bat",
        "cat",
               ]

~~~
jashkenas
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.

    
    
        list: [
          "ape",
          "bat",
        ]
    
        list: [
          "ape"
          "bat"
        ]
    

.. are the same.

~~~
arohner
And in Clojure, all commas everywhere are whitespace. You can use them, or not
use them, even on single line expressions.

(list a b c) == (list a, b, c) == (list a b, c)

------
chokma
"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
      .

------
pradocchia
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.

------
rbonvall
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:

    
    
        var a = "ape"
          • b = "bat"
          • c = "cat"
          • d = "dog"

------
Pheter
"Note how the errors pop out in the comma-first style."

So what? Any decent IDE will highlight this error for you anyway.

~~~
edanm
Not the ones I use.

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".

~~~
Pheter
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?

~~~
edanm
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.

------
tetha
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.

~~~
j_baker
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?

------
edw519
I always thought that code is like babies' diapers.

I'll change my own. I'm so used to the smell, it don't even notice it anymore.

You change your own. It smells so bad my eyes are watering.

~~~
mmelin
And young people who have not had kids yet think that every diaper they come
across needs changing.

Good analogy.

------
dmoney
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.

------
vital101
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.

------
k33n
These examples are ridiculous.

~~~
mrvir
Exactly, seeding a long array has never been pretty. Some languages allow you
to get rid of the comma but that's about all. Get over it.

------
mvandemar
I disagree with the title statement, as there is little in life more
subjectively judged than beauty, but the post itself is very well written.

------
IgorPartola
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>.

------
Tichy
All code is ugly, but some code is uglier than other code.

------
st3fan
Not mine.

------
apphacker
jslint solves all of these problems.

------
david927
_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.

~~~
jsankey
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.

~~~
david927
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.

We can do better.

