Monthly Archive for March, 2009

Refactoring: Improving the Design of Existing Code

58278251 f8a185fdc7 m Refactoring: Improving the Design of Existing Code
Image by seizethedave via Flickr

You may say I’ve been reading a lot recently. And you can be right saying that icon smile Refactoring: Improving the Design of Existing Code

I’ve just read Refactoring: Improving the Design of Existing Code by Martin Fowler. I was really looking forward to reading it and I must say it was worth.

Mr Fowler started the book with an example of how to refactor poor code that is hard to read. He showed the refactoring process step by step pointing out the benefits of the changes. Only then did he start with the theory: definitions, why, when, what to refactor, possible problems, etc. What I liked was he admitted the importance of unit testing the changes (short introduction to JUnit), which can confirm they didn’t break existing logic.

But the real core of the book were the chapters that described in details different aspects and areas of refactoring: composing methods, moving features between objects, organizing data, simplyfing conditional expressions, making method calls simpler, dealing with generalization, and big refactoring. Each item was a separate chapter and contained a number of refactoring techniques. All techniques delivered in an easy to read form – motivation, mechanics, examples in UML and code).

I learned a lot and brushed up a lot. Even although some aspects seemed to be obvious and clear I got the naming and fresh look on them.

Highly recommended.

 Refactoring: Improving the Design of Existing Code

MonoRail – RenderMailMessage – System.ArgumentNullException: Value cannot be null. Parameter name: format

This was a nasty issue…

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException:
Value cannot be null.
Parameter name: format
   at System.String.Format(IFormatProvider provider, String format, Object[] args)
   at System.String.Format(String format, Object arg0, Object arg1)
   --- End of inner exception stack trace ---

Background

This is part of the stack trace I got in one of my applications which uses MonoRail. I got it while creating the Castle.Components.Common.EmailSender.Message in order to prepare the content of the email having the name of its template file (vm):

Message msg = RenderMailMessage(templateName)

That view file defined the content and used data from PropertyBag and from Resource files.

Just to recall a resource file (resx) is bound with the controller class with this definition:

[Resource("text", "LocalizationSample.Resources.Home")]

What was wrong there? I was sure templateName passed as the parameter was correct – it for sure pointed to correct vm file. Moreover, that piece of code was defined in a superclass which was extended by this particular controller and another one which also could send this email. Of course there was no problem with sending email by the latter.

Solution

The problem here was I used this construction in the vm file:

$string.format($text.someText, $param1, $param2)

And for some reason I forgot to bind the appropriate resource file (the one referenced with $text) with one of the controllers. As a result string.format failed because $param1 and $param2 couldn’t be injected into string which was not found.

Head First Design Patterns

Another book I recommend reading: Head First Design Patterns.

I need to say I only managed to read it when I tried for the second time. First time I gave up after three or four chapters. Although I believed the provided definitions, examples, code snippets were good, I was fed up with all this Zen talks, interviews with design patterns, and even their chit-chats.

However I decided to give it a try. I finished reading this book yesterday and I need to admit I’m very happy I did it! The book covered the most popular design patterns (e.g. decorator, abstract factory, proxy, observer) and presented them by showing how existing code can be improved by introducing them. It also reminded several object-oriented programming rules (e.g. polymorphism, Law of Demeter), meaning of Gang of Four, and good practises of using design patterns (when usem and when not). Now i need to say event design patterns chit-chats were crutial as they were supposed to repeat and consolidate given study.

Highly recommended.

 Head First Design Patterns

Agile Project Management with Scrum

Agile Project Management with Scrum by Ken Schwaber is a book I read a couple of months ago. I had never been involved in an agile project and I had been hearing “agile”, “scrum” for some time, and didn’t know what they all were about. Then one of my colleagues lent my that book.

I was really happy to read it because the author not only described mere facts (theory on how agile with scrum works) but also provided a number of life examples – real life problems and how introducing agile aprroach helped to fix them.

I recommend reading this book if you want to be quickly introduced to agile projects that employ scrum.

 Agile Project Management with Scrum

Summary of Communities to Communities (C2C) 2009 Conference

C2C is a history now. I attended .NET stream and one session on SQL. The conference as a whole was very interesting, well organised, and basically developing. It was a Polish conference so most sessions were conducted in Polish but there were a couple of speakers from abroad too, and they spoke in English.

Basically I’m happy I had a chance to attend the conference. Below are my notes and comments. Please note they are my comments; should I write something which is not true or accurate, please let me know, sometimes pace was fast so I might have skipped something icon smile Summary of Communities to Communities (C2C) 2009 Conference
Continue reading ‘Summary of Communities to Communities (C2C) 2009 Conference’

IE7 – problems with onchange event for text field when auto complete used

Imagine you have a text field in the form which is defined as below

<input type="text" name="fake" id="fake" onchange="doSth()" />

Apparently doSth() will not be called at all if you are running IE7 and use its auto completion mechanism. If you bind doSth() with onblur that won’t solve the case either.

Solution

The best solution is to call doSth() only when the form is submitted. That seems to be the safest option.

Unfortunately, there might be cases when doSth() must be called immediately after leaving the input, not only at the submit time. Any ideas here?