Fog Creek Software
Discussion Board

Writing Perfect Code?

For many weeks now, I have been writing near-perfect code, the first time.

What I mean by this is that I have been in "the flow" to such an extent for such a long time now that when I need to write function X or write query Y or build screen Z, I've been able to do it almost effortlessly and with a seemingly impossibly-low error rate.  The code just pours forth, fully-formed, along with some of the most rigorous unit tests I've ever written that **invariably** beat that code to death without a single error.

This is in huge contract to how I coded just six months ago or so, where (comparitively) I was stumbling and falling over myself to get even moderately complex modules built.  It seemed that I was proceeding in a "normal" way then, on par with some of my sharpest coworkers.

I've started to worry quite a lot about this, mostly that the code that *seems* right, which sprang fully-formed from my fingertips, is actually riddled with fatal bugs that will rear up and devestate my company's reputation.  But some of this "perfect" code has been in production for almost five months, being used by thousands every day without so much of a peep of complaint.

I'm wondering if I'm experiencing delusions of grandeur, or if this is normal after one has gained a certain level of experience.  FYI, I have about six years of relatively broad experience, and much of that was spent designing large systems as well as developing them.

So, has anyone had similar experiences, or am I just crazy?

Sunday, March 2, 2003

When you have a solid grasp of what it is you need to write, it usually ends up being easy to code as you mentioned.  OTOH when you don't have a good grasp of what needs to be done it is then a drudgery.  This has happened to me many times.  A good attitude and perseverance are more important than the fickle, "The first time I code something I got it right."

Also Anon
Sunday, March 2, 2003

Whnever I code something and it compiles AND runs correctly the first time I get nervous. I know I must have missed something.....

Sunday, March 2, 2003

Strange how that happens.  I was able to do to something like this.  I think what happened is that I was just aware of each function/method's inputs and outputs.  My thinking was just simpler, as if I stripped out all those extraneous thoughts that lead to confusion.

I can occasionally do this now in those little python utils I make for myself (right now I'm just studying) and the only time I'm tripped up is when I use one of those destructive functions that return nil.  As you might be able to tell, I hate state destruction except as an optimization...

Sunday, March 2, 2003

You were in the zone, grasshopper.

Now all you have to achieve is calm acceptance of the inevitable when something you couldn't possibly forsee means you have to rewrite it.

Simon Lucy
Monday, March 3, 2003

As for me, I like to code having _no fear or doubt_, especially while dealing with pointers in C. That may produce bad or good code, but, anyway, I really like being in that state.

Monday, March 3, 2003

When things are this sweet and I'm coding totally error free (apart from spelling mistakes) for a long period of time, I find I get very bored with everything. Things just doesn't push or motivate me anymore.

But after a while I come back down to earth, out of the zone, and start making mistakes, design errors and even total foul ups costing the company hundreds.

So be careful, things could change.

Stephen Hassall
Monday, March 3, 2003

A novice asked the Master: ``Here is a programmer that never designs, documents or tests his programs. Yet all who know him consider him one of the best programmers in the world. Why is this?''

The Master replies: ``That programmer has mastered the Tao. He has gone beyond the need for design; he does not become angry when the system crashes, but accepts the universe without concern. He has gone beyond the need for documentation; he no longer cares if anyone else sees his code. He has gone beyond the need for testing; each of his programs are perfect within themselves, serene and elegant, their purpose self-evident. Truly, he has entered the mystery of Tao.''

-The Tao of Programming  2.4

Monday, March 3, 2003

Woe to the ignorant soul who does not test their code, who breaks the build and snifs "it worked for me".  For they shall be smacked down badly and brutalized.  The scorn of their peers will shame them.  Humiliation will be their toil; suspicion and avarice will surround their name.  Their code will be discarded in the next rewrite and their peer review scathing.  Such is the tale of wayward coders who ignore the unit test.

Nat Ersoz
Monday, March 3, 2003

For the original poster:

Are the other aspects of your life going particularly well right now?

It's been my experience that the parts of one's life are affect each other and are self re-enforcing. If everything else is going great, then  I'm happy and I produce good code. If I'm miserable outside of work, works not going to go as well.

Something like this might be a factor in your recent productivity increases.

Bill Tomlinson
Monday, March 3, 2003

Nat: well said. I gotta quote that for my tirades on the infidels who disgrace their code with lack of unit tests :-)

Ian Stallings
Monday, March 3, 2003

With me the "zone" is a time zone. I once noted the preponderance of bugs in code checked in during the afternoon or evening. So now when I finally track down that elusive bug at dark o'clock in the evening, I smile, walk away from the keyboard, and fix it in the morning.

Willy Mills
Monday, March 3, 2003

He, I am experiencing this right now and I find it suprising.

I think that this is how coding should be like. It should just work. No leaky pointers that makes your program crash. No shared library that you can't load on platform xyz although it works on all the others.

One of the poster is true, somehow it is boring. Things just work you know. On the other hand, it is very rewarding because you progress quickly.

Small advocacy, I have enjoyed this more often when coding in python.

PHilippe Fremy
Monday, March 3, 2003

I've had a similar experience, but usually it has been when I've had such a clear view of the design, and the design was *right*(tm) that coding it was as much a matter of "describing the design in compilable text" as anything.

I've had a recent project go that way (coincidentally, also built in python, due to recent scarring from perl's "memory management") and the metric for how good it turned out wasn't just "it all flowed nicely" but that "an unforeseen change took a day of thought and 5 lines of code (and 10 lines of test cases)".  The system just abstracted much more cleanly than I'd expected; maybe I'm more in tune with the particular project than I had been before, maybe more of the design is running in the back of my mind, maybe python is more cleanly expressive... or maybe my pants just fit better :)

Certainly one of those "if I could bottle that..." experiences; I'd be pleased if I could teach others how to achieve it as well, then I could retire again instead of getting lured into new projects :-)

Mark Eichin
Monday, March 3, 2003

Someone asked how things were going in my life in general, with the theory that personal and professional success or progress might be a feedback loop, etc.

Things have actually been really shitty over the past four months or so.  Serious illnesses coming down on multiple family members, some money problems here and there due to unforeseen expenses, etc.  So, at least in my case right now, there's no karmic harmony going on between work and home.

A couple of people seem to have implied (or perhaps straightforwardly suggested) that I'm not doing the proper testing, and that's why everything is going so swimmingly.  That's just funny.  If anything, I've always gone overboard with unit testing, and my recent state has caused me to become more paranoid than ever, resulting in even more, larger, and excessively obscure test cases being written, run, and passed without problems.

Eh, but I'm getting a headache this afternoon, so maybe I shouldn't have said anything.  I NEVER do well with headaches. :-)

Monday, March 3, 2003

No, that wasn't targeted at you.  That was targeted at that Tao of programming thing.  I wanted to blow that thing up like the Talliban blows up Buddha statues.

Infidels:  what an appropriate term.  :)

Nat Ersoz
Monday, March 3, 2003

One unintended consequence of "being in the zone" and whipping out code, is that when I go to work on it months later, it is basically unfamiliar to me.  It's almost like blacking out while writing it, so I have no concious memory of it or writing it.  Granted the code is well written (as a rule), but it's still "unfamiliar".

Peter Montgomery
Monday, March 3, 2003

*  Recent Topics

*  Fog Creek Home