10

I've come 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" our code too much, attach our-self to it, making us more vulnerable to bugs and other mishaps that may be lying in front 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?

1
  • 7
    After your first million lines, you stop being particularly attached to any specific ones. :-) Sep 18, 2009 at 20:24

11 Answers 11

34

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.

7
  • 5
    The concept of "sunk cost". Possibly not a totally new idea. Sep 12, 2009 at 12:34
  • 1
    Mmm. Sunkist. Wait... oh, cost. Right.
    – Artelius
    Sep 12, 2009 at 12:41
  • 4
    @Adrian: No, not a new idea, but one that every generation of humans struggles to grasp because it goes against the grain. +1. Sep 12, 2009 at 13:19
  • 6
    +1 for the Lemmings avatar. Sep 12, 2009 at 13:29
  • 2
    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
    Sep 14, 2009 at 14:50
14

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.

5
  • +1 for the Einstein comment to Guffa's answer.
    – Artelius
    Sep 12, 2009 at 13:40
  • ... so everything you make is uniformly not good enough to keep. So why create?
    – Robert L
    Sep 17, 2009 at 19:25
  • @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. Sep 17, 2009 at 21:24
  • 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. Sep 18, 2009 at 18:10
  • 2
    Robert L: for the Process, not necessarily the Product. Sep 18, 2009 at 20:25
8

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.

0
6

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

6
  • 7
    Einstein is totally overrated. What made him so smart, aside from relativity and leaving Germany in 1933? Sep 12, 2009 at 13:27
  • 6
    @MusiGenesis: You mean what made Einstein so smart, other than revolutionising our understanding of the physical universe? :-P Sep 13, 2009 at 6:02
  • 4
    "...and what did the Romans give us?" (tongue firmly planted in cheek)
    – LaszloG
    Sep 15, 2009 at 20:03
  • Yeah. And Newton, what's so great about him? Everybody already knew that things fall down. :-) Sep 18, 2009 at 20:27
  • 1
    @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
    Nov 25, 2009 at 10:15
3

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!

2

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.

2

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.

1
  • +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. Sep 13, 2009 at 6:04
1

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?

1

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

0

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.

0

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?).