Coding by intellisense thinking

I´ll start this post off by again apologizing for my absence from the “blog scene” lately.
My explanation is simple, it´s been a long and warm summer in Sweden and I haven´t actually had all that much inspiration lately.
But, now I have started my new job at Avega and I hope the fall will again inspire me to write new posts about things that interest me. I have also started to “update” my codeplex project SharePointContrib with some new stuff and I´ll probably migrate it to VS2010 hoping to test it more with SP2010.

In this first post after the summer though I will talk about something that I have named “coding by intellisense thinking”.
The source for this “practice” comes from the TDD style of “coding by intention” (aka “coding by wishful thinking” as I first heard of it).

The core concept of it is that when you write a piece of code you write it like you “wished” it looked like and not by thinking “hmm in order to do this I have to implement that first” etc…more of a top/down approach opposed to bottom/up.
That´s pretty much like the TDD way of first writing the test, then make it fail, then make it pass with as little effort as possible and finally refactor.

In my “practice” I´m thinking along the same lines but since I´m using Visual Studio 2010 I´m going to use the new intellisense features to do sort of the same thing for me.

An example

Let´s say that I wanted to write a little program that can read some data (in this example xml) and transform it to some known entities (books in this example). Then I would like to be able to ask for a specific book and write out the details of that book to the console. Let´s start…

“I wish I had a book repository”


In VS2010 you can write code like the one seen above. I don´t have to construct the classes before using them instead I get an option to have that done for me…

Clicking on the red squiggly (or CTRL + .) pops up that helper for me.

“I wish the book repository had a FindById method”

In the same manner I can call methods on the class that doesn´t already exist and tell VS2010 to generate them for me.

“I wish the book entity had a method that can give me the details of that book”

This shows the “final” code for the console application (at least it compiles). I have yet to implement the other stuff in the application but the skeleton is complete (aside from refactoring it later perhaps).
Now let´s continue with that details method on the book class that I wished I had.

“I wish the book entity had an ID property etc”

Now I let VS2010 fill my entity with properties for the ID, Title, Author etc much in the same manner as before.
Note: You might have to correct some of the types for the properties (like datetime) but overall it still works.

Here is what the final book entity might look like:

Now let´s continue with that book repository and the FindById method.

“I wish I had a collection of all the books in the repository”

In order to complete the linq query above I have to get access to all the books. VS2010 intellisense can´t help me out here…I actually have to write the Books getter myself…bah.

And now I have to “wish” I had that FindAllBooks method implemented.

“I wish I had a repository that could hand me the xml files to parse”

“I wish I had a data mapper that can translate from xml to a book entity”

The final data mapper looks like this:

And finally the xml repository looks like this:

Note: I could probably do some refactoring here to…leave that for later.

OK, so now my little program works and now I can start over by refactoring it I “wished” to do so.

Conclusion

My experience is that working like this (having the mindset) makes my code much cleaner and robust.
I often use this approach with extension methods on classes that I work with, like “I wish this class had this method” and then implementing it.

// Happy coding

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: