ALL code is ugly
gist.github.com
gist.github.com
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 <)))
}
} def qsort[T <% Ordered[T]](list:List[T]):List[T] = {
list match {
case Nil => Nil
case x::xs =>
val (before,after) = xs partition (_ < x)
qsort(before) ++ (x :: qsort(after))
}
} qsort [] = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs) 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.http://www.informit.com/articles/printerfriendly.aspx?p=1407...
(Not that recent, I guess. Nov 2009.)
On the other hand, "make sure that there's as little code as absolutely necessary" is absolutely my prime directive as a programmer.
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.
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.
Good analogy.
mylist = [
"ape",
"bat",
"cat",
] list: [
"ape",
"bat",
]
list: [
"ape"
"bat"
]
.. are the same.(list a b c) == (list a, b, c) == (list a b, c)
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
.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.
var a = "ape"
• b = "bat"
• c = "cat"
• d = "dog"So what? Any decent IDE will highlight this error for you anyway.
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".
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?
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.
My humble attempt to find some code that's pleasant to look at: http://lovelyco.de.
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.
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.
What is this solution then? (It better not involve XML or Excel spreadsheets).
I'm struggling to see what is stupid about it.
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.
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).
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.
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.
We can do better.