tags:

views:

649

answers:

11
+11  Q: 

sentimental code

I've came across an article that discusses the issue of "code admiration". Basically, the author talks about how developers should be more skeptic about code they write. How we may "admire" too much our code, attach our-self to it, making us more vulnerable to bugs and other mishaps that may be lien in from of us.

How do you feel about this issue? And do you have more tips on how to avoid/be more aware of this issue?

+29  A: 

Some years ago, I was working with another on a small "hobby" project, and I realised that we had to re-assess things. We had written lots of code but it wasn't all good code.

We didn't really want to "throw away" all the work we had put in. But I realised something: what mattered most was the amount of work we would need to put in from now on.

We couldn't change the fact that we had already put so much work into the project, so the only way to minimise the total amount of work the project would need, would be to minimise the amount of work we hadn't done yet.

Since that day, I have stopped being attached to my code. If I'm confident that throwing it away and starting from scratch means less work than keeping it and adapting it to my needs, then I'll throw it away.

Artelius
The concept of "sunk cost". Possibly not a totally new idea.
Adrian Pronk
Mmm. Sunkist. Wait... oh, cost. Right.
Artelius
@Adrian: No, not a new idea, but one that every generation of humans struggles to grasp because it goes against the grain. +1.
j_random_hacker
+1 for the Lemmings avatar.
MusiGenesis
Awwww, but it's a community wiki :(
Artelius
@Adrian: Maybe not a new idea, but does it ever have a sway over people's decisions.
bigmattyh
I've found tools like ReSharper are very helpful to dissociate yourself from your code. When a keyboard shortcut can refactor an entire project you begin realise that your thousands of keystrokes arent all that important.
Alex
+5  A: 

We sure like to admire our nice code, but it's not always easy to know what to admire. Complicated and elaborate code is sometimes mistaken for admirable code, while elegance and simplicity should rather be what to strive for.

Two quotes come to mind:

"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

and

"Make everything as simple as possible, but not simpler."

-- Albert Einstein

Guffa
Einstein is totally overrated. What made him so smart, aside from relativity and leaving Germany in 1933?
MusiGenesis
@MusiGenesis: You mean what made Einstein so smart, other than *revolutionising our understanding of the physical universe*? :-P
j_random_hacker
"...and what did the Romans give us?" (tongue firmly planted in cheek)
LaszloG
Yeah. And Newton, what's so great about him? Everybody already knew that things fall down. :-)
RBarryYoung
@RBarryYoung: Newton taught us the importance of self-promotion and claiming individual credit for group accomplishments (poor Leibniz!).
MusiGenesis
@j_random_hacker: Penicillin was a revolution in the treatment of disease, and it was discovered *by accident*. Just because you have a huge impact doesn't (necessarily) indicate that you are clever.
Artelius
+2  A: 

I never admire my code. I admire other peoples code that i "borrow" and try and emulate them or better them and i find that the more i know, especially about coding the more i find i don't to know. The only thing of value wold be for peer programmers to admire my code and borrow it.

PurplePilot
+5  A: 

I post a fragment from Jeff Atwood's blog, Sucking Less Every Year, and I agree 100%.

I've often thought that sucking less every year is how humble programmers improve. You should be unhappy with code you wrote a year ago. If you aren't, that means either A) you haven't learned anything in a year, B) your code can't be improved, or C) you never revisit old code. All of these are the kiss of death for software developers.

Nick D
+1: that's very true
Atmocreations
+11  A: 

My high school art teacher used to encourage us to take what we considered to be our best drawings and tear them up; he called this "cleansing the soul". His reasoning was that, as artists, we were driven to create works of art, and any time we produced something that we liked and that gave us satisfaction, our motivation to continue creating would be lessened.

So I followed his advice and tore up my best stuff, and it worked. Instead of spending my time admiring my old work, I created new stuff and continually got better. I've tried to follow the same principle with my code, but it doesn't really work: my computer has a tough plastic shell that is nearly impossible to tear through.

MusiGenesis
+1 for the Einstein comment to Guffa's answer.
Artelius
... so everything you make is uniformly not good enough to keep. So why create?
Robert L
@Robert L: you don't keep tearing up your best stuff *forever*. If you get good enough, you can start selling it instead, which accomplishes the same thing.
MusiGenesis
That would be great until you get tired of redoing the same thing again and again and again (and different every time) for every new client because you gave your creation's rights to your previous clients, and you cannot use it anymore.
Stefano Borini
Robert L: for the Process, not necessarily the Product.
RBarryYoung
+2  A: 

I think he has a good point. It's frustrating to work with people that have too much of this, as it really hinders teamwork and getting to the best solution to the problem.

As I can be a bit delusional, I try to put practices in place that will keep me grounded in reality. For code,

  • unit tests: These keep me more focused on what the code is supposed to do, as opposed to any abstract "beauty".

  • shared code ownership: There are two camps here: give people more ownership of their code and hope pride takes over, or give them less and let peer pressure come into play. I believe that giving people more ownership can lead to this code admiration. We practice shared code ownership, so I am constantly forced to see someone rewrite my perfect code to make it better (in their mind). I quickly realized admiring it too much was a waste of time and emotionally difficult.

  • pair programming: working side-by-side with someone will keep you realistic.

  • other feedback: These are all feedback loops, but there are others. There's no better way to see if something works than by watching someone (try to) use it. Put your work in front of as many people as possible. Have code reviews. Read other people's code. Run static code analysis tools.

ndp
+1, though I'm not sure that shared code ownership will work for everyone. You're right about the tradeoff between pride and peer pressure though.
j_random_hacker
A: 

Two words: code review.

Gather two or more fellow developers and invite them to review/criticize/comment on your code. 'twill shed some (admittedly harsh) light on your code.

LaszloG
+1  A: 

I'm with PurplePilot - I don't admire my own code, and as such I'm constantly searching for new, more efficient (hell, easier) ways of doing the same thing. I like the Effective c# book, picked up lots of useful code from there that I admire.

I would have no hesitation about throwing code away and starting again, but not necessarily from scratch, i.e. by writing some code for a specific scenario and then throwing it away, you'll probably have a better grasp of the scenario. In other words, it's a "wicked problem", or you've found another way that doesn't work a la Edison.

It begs a wider question: if code isn't thrown away, or at least revisited, is developing on libraries that are becoming stagnant a good thing?

Mike Kingscott
+1  A: 

There is nothing wrong with admiring your code ... this is part of the positive reinforcement process that will motivate you to write more and better code in the future.

However, misplaced or misused admiration can be a problem. If the code is really not good, or has bugs that haven't been exposed by unit or other testing, or needs refactoring/redesign/replacement then this misplaced admiratoin is a problem. And using admiration as an excuse to skip part of the process - such as code reviews, or not having a skeptical attitude towards code - is misuse of admiration.

Like anything else that is good, admiration of code can be misplaced or misused - it doesn't mean that it in itself is bad. That would be like saying "religion is a bad thing, because it causes conflicts and wars between people".

Larry Watanabe
A: 

It's perhaps better to have a healthier perspective - we aren't rocket scientists, and we aren't curing cancer - it's just work.

(Yes, it's reasonable to be proud of an entire building you helped build if you're an architect, but do they really have a lot of their self-esteem wrapped up in an individual blueprint, or a closet on floor 3 they designed by themselves?).

M1EK
+2  A: 

Jonathan Edwards wrote an impressively beautiful essay on this subject, prompted by the work on the O'Reilly book Beautiful Code. Here's the final paragraph, but the rest of the essay is also worth reading.

Another lesson I have learned is to distrust beauty. It seems that infatuation with a design inevitably leads to heartbreak, as overlooked ugly realities intrude. Love is blind, but computers aren’t. A long term relationship – maintaining a system for years – teaches one to appreciate more domestic virtues, such as straightforwardness and conventionality. Beauty is an idealistic fantasy: what really matters is the quality of the never ending conversation between programmer and code, as each learns from and adapts to the other. Beauty is not a sufficient basis for a happy marriage.

Other versions of this same wisdom exist in other fields. Samuel Johnson, about writing:

Read over your compositions, and wherever you meet with a passage which you think is particularly fine, strike it out.

William Faulkner's version of this was much more succinct: “Kill your darlings.”

My father-in-law works as a film editor, and he studiously avoids the set where the film is being shot. When he does have to visit, he shields his eyes as much as he can. This is because when he decides whether or not to include a scene in the final film, he doesn't want to be influenced by how much effort it took to shoot the scene. What matters is how well the scene works in the final film.

My essay, "My evolution as a programmer" (which I would link to if I weren't a new user), is largely about learning skepticism about the code I'd written: whether it works, whether it's useful, whether it's comprehensible (pair programming was a real wake-up call here). It's hard!

Kragen Javier Sitaker