6 ms·
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 ent
by mhansen 16y ago
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 <)))
}
}
- warfangle 16y agoI still like the pattern matching version, better. It's more concise, and I can actually follow it easier. (but my brain is weird): 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)) } }
- jedbrown 16y agoJust 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.
- _pius 16y agoTouché.
- _pius 16y ago... which is butt-ugly compared to QuickSort in Haskell: qsort [] = [] qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)
- ramchip 16y agoYes, 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 16y agoWhile 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.
- gruseom 16y agoInterestingly, this very passage of code is subjected to a withering critique in Alexandrescu's recent article on iteration: http://www.informit.com/articles/printerfriendly.aspx?p=1407357 http://www.informit.com/articles/printerfriendly.aspx?p=1407... (Not that recent, I guess. Nov 2009.)
- _pius 16y agoNice article, thanks!
- andywood 16y agoI 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.
- arethuza 16y agoAgreed, it never ceases to amaze me how some developers seem to love writing so much pointless boilerplate code.
- edanm 16y agoCode 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.
- docgnome 16y agoI'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.