Beautiful Code
alarmingdevelopment.org
alarmingdevelopment.org
In order to handle complexity, I split my program carefully into modules with low coupling. I'm happy until a new feature comes along which cannot be implemented without destroying the independence of the existing modules. Then there are two options: Either I redesign the system based on the new requirements or hack the new feature into the old architecture. Both alternatives make me sad.
I recognize some repetitive pattern in my code. So I create some straightforward abstraction that removes the redundancy. I'm happy until the pattern appears again, but this time in some slightly different form so that the old abstraction can't handle it. Then I change the abstraction to include the new case. Eventually, I realize that the abstraction has only complicated the system, because now one has to understand both the implementation of the abstraction and the use of it. This make me sad.
I'm working on some easy task. I know that I have solved problems like this many times before. Unfortunately this time my brain just refuses to solve the problem. Without solving the problem, I go home and think about pursuing a different career.
On the next morning I solve the problem within 5 minutes and I'm glad to be a programmer.
I can't count the number of times this has happened to me. Never underestimate the effects of a good night's rest.
That's a real problem. The best abstractions are so useful, that they even help understanding when you only use them once.
E.g. functions come to my mind --- because they have clear entry and exit points (of control and communication), they can be easier to understand than the same code inlined.
(Functors and Monads also seem to enhance my understanding.)
I am just saying this because I totally relate to the article,it is often very hard to contain or abstract complexity. or it requires nearly equally complex mechanisms or abstractions.
It's OK for functions to grow more abstract. Just provide the simple things, too.
E.g. you start out with `map' and `filter', but then realize that `foldr' can express both of them and much more. One viable path is to rewrite the definitions of `map' and `filter' in terms of `foldr'. One should not use `foldr' everytime in the code where a `map' or `filter' suffice.
One can layer abstractions and then use the most specific tool possible that still has all the flexibility you need at each point.
Having more powerful abstraction techniques, like they are possible in languages like Haskell or Scheme/Lisp, tends to give less leaky abstractions for me.
Functions cover a lot of contexts (in languages that support them properly and where they are not a syntactic burden).
the trouble is abstraction can solve all problems except too much abstraction.A good piece of OOP code is verbose yet simple. It may be a world of nouns, but there's a little tiny chunk of code in there that does one thing and does it well. Beautiful OOP is made for morons. And I mean that as a compliment.
FP, on the other hand, couples the data structures so tightly in with the code that I'm always the human linker -- keeping track of dozens of different things at the same time. But when done well, all of this incredible amount of work just falls out of a line or two of code. You have to keep track of the pieces, but your opportunity to combine them in cool ways is incredibly higher. Beautiful FP is like watching origami -- from a simple base somehow something beautifully complex emerges.
Then there's the difference between beautiful startup code -- you have no idea of the problem so it is able to pivot in multiple directions quickly, and beautiful commercial code -- you know the problem and you've met the solution in the most maintainable, understandable way possible.
The term "beautiful" is overloaded too many times. And good coders aren't looking for artistic perfection. I agree with the author: it's a bad idea.
I don't know that I agree with this. Code is code. Now, I will agree that you may have to take a different approach to writing beautiful code in OOP vs FP. But I don't think that what makes functional code beautiful is different than what makes object-oriented code beautiful.
In all, I feel that beautiful code is (in this order):
* Something that gets the job done.
* Something that gets the job done correctly (there is a difference).
* Something that tackles a complex problem in an easy-to-understand way.
I agree that we as programmers shouldn't seek perfection when no perfect solution exists. However, it's usually in your best interests in the long term to put forth the extra effort to make your solution beautiful where it's feasible.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Many of us already have.
The lack of a Universal Theory of Software Design != "no general principles or techniques".
Anyone programming today is standing on the shoulders of giants. Their trial and error is our starting point. No, they didn't figure everything out, but it they had, programs would be written by other computers, not us. Maybe someday, but I, for one, am not quite ready for that.
"Beautiful code is like obscenity. You know it when you see it."
http://stackoverflow.com/questions/20564/what-constitutes-be...
http://stackoverflow.com/questions/563036/what-is-elegant-co...
But every happy marriage is founded on that each finds the other beautiful.
If you at least get the foundation of the system "beautiful", when the abstractions start to leak, as they invariably will, you can manage them to a much greater extent. You're right about not expecting the simplicity and elegance to remain, but there's a huge difference in manageability toward the end of each system's life. Get the foundation right; the house will follow.
Really? That doesn't accord with my experience, or any actual research that I've read. The conclusion of the research that I've read is that appearance generally matters more to men, and less to women. And anecdotal experience suggests that the initial infatuation fades in perhaps 7 years or so, and after that looks don't help that much.
Not that you're likely to believe me. After all I've only been married 20 years, so what could I know?
And this confusion only goes to prove the original point, about beauty being illdefined.
I think his/her reaction will disabuse you of that notion.
I told my girlfriend that I didn't get together with her for her looks yet she married me anyways. That was 20 years ago and we're still married.
If his point was that beautiful code is rare and only refined through a long iterative process perhaps he could've found some code that was initially average or even bad and showed what each incremental refactor/rewrite did to it until it became "beautiful".
My last boss told me "there are programmers who produce code with absolutely no bugs or problems". I would prefer no to give that kind of misconception any more ammunition.