Dream coding a Composition

April 14, 2004 on 9:21 pm | In General |

You know you have been coding too much when you start dreaming about it. Not exactly about someone (yourself or anyone else) coding, but about code itself. I remember that when I was preparing for the CPE (Certificate of Primary Education) exams, I used to have numbers and math operations flying around in my head; I would lie down in bed at night and do mental calculations till I fall asleep.

What happened to me this morning as I came out of sleep was very similar, except that I was manipulating the code I am currently working on. What still amazes me is the fact that I actually uncovered an anti-pattern in a piece of code that I had not touched for weeks. Although it may be funny that the word “Eureka!” flashed through my mind at the same time, it is wonderful that such a thing could happen. The anti-pattern in question is as follows.

I have a business class called User that is a composite of another class called ContactInfo, itself a composite of the Address class. What currently happens in my code at the time a user needs to be added is:

1. Instantiate an Address object.
2. Instantiate a ContactInfo object passing the Address object to the constructor.
3. Instantiate a User object passing the ContactInfo object to the constructor.

What my dream told me to do (which I am going to tonight) is:

1. Instantiate a User object.
2. Within its constructor, instantiate a ContactInfo object.
3. Within that object’s constructor, instantiate an Address object.

Of course, this is the correct way to proceed according to the composition rule, which says that a composite object should be responsible for the creation and lifetime of its components.

I am not very sure whether such dreams are healthy, or whether they are a precurse of dementia. But, I more than welcome such inspiration.

Related Posts:

3 Comments »

RSS feed for comments on this post.

  1. Solving problems during sleep-time is a conscious routine for me.

    I suggest myself to resolve the enigmas at hand before going to sleep. I usually have the answers or several leads at wake-up time the next day.

    I solved loads of coding (and other) problems this way.

    In the morning, the answers I get seem so obvious or obviously right.

    La nuit porte conseil.

    Comment by Yash — 11 May 2004 #

  2. Everyone has a sweet spot where (s)he gets inspired more. My favourite ones are 1) in the shower, 2) in front of the sink while washing dishes and 3) in trains.

    I tried thinking about problems when lying in bed, but lately, I do not even have time to formulate the problem that I have already fell asleep!

    Comment by Eddy Young — 12 May 2004 #

  3. That’s nice ;-) I normally get my inspirations under a hot shower running for 20 mins.

    Comment by Arvind Doobary — 13 May 2004 #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>





Powered by blog.mu with Pool theme design by Borja Fernandez.