Saturday, October 28, 2006

Refactoring: Replace Method with Method Object

Well, reading Refactoring book from Martin Fowler, I feel some case of the refactoring I have used before without realizing it. The most interesting I have found so far is Replace Method with Method Object. The idea is if your method is too long, you should find a way to shorten it to make it more readable and self-explained. Well, self-explained can be done using comment in the code. But sometimes comment can be over killed too. Some expert suggest that too much comment means your code is not refactored well-enough. So, in the book, it suggest you to use Extract Method.

I hope below sample can explain what Extract Method, although it's too simple:
void LongFunction(...)
{
// very long function here
foreach(int item in items)
if (item % 2 == 0)
isEven = true;
// another very long function
}

Applying Extract Method, it will be:

void LongFunction(...)
{
// very long function here

foreach(int item in items)
isEven = IsNumberEven(item)
// another very long function
}


bool IsNumberEvem(int item)
{
if (item % 2 == 0)
return true;
else
return false;
}

Ok, most of the cases Extract Method can be used, as Fowler said, 99% cases. But some extreme cases, that you use too much local variable will be very difficult to apply Extract Method, another ways to do are Introduce Parameter Object, Preserve Whole Object, and Replace Method with Method Object.

Well, what I have experienced before is I have a function that is to do Invoice Calculation. This Invoice Calculation include bunch of variable, local object, etc, and quite long of code and logic. This calculation is used in so many places, like Creating Invoice, Check GIRO, Apply GIRO, etc. So, with Replace Method with Method Object, I create totally new class to do Invoice Calculation. All temporary variable and local object are created as class fields/properties. After applying all the logic into the new method inside the new class, I start to refactor using Extract Method. After series of retesting, I found the new code is far easier to read and maintain. The InvoiceCalculation object can be reused easily, without using too many local variable/paremeter. And, another aspect, I can focus on tweaking the performance easier.

The interesting point, I did this before reading the refactoring technique. After reading, I can apply more understandable word to explain what I am doing. Good book, try yourself.

Thursday, August 17, 2006

Javascript: Debugger

Although I already quite spent times at coding Javascript, I just knew this useful feature in Javascript, that we can put debugger keyword at new line of the code (without any semi colon) that will function like breakpoint. If your browser has already set to enable debugging (Tools > Internet Options > Advanced > Uncheck Disabled Script Debugging, then when the browser execute that line, it will prompt you want to debug the script using any debugging tools (VS2003/2005, etc).

Sunday, July 23, 2006

MS CRM 3.0: How to display Active only at Associated View.

For you do customization on Ms CRM, by default, Ms CRM 3.0 will display both active and inactive record in associated view (View that is displayed child entities in the detail of master entity) for Custom Entities. This is different from default associated view of Contact that is displayed in Account detail, which just displays active record. To achieve same view as Contact, you must edit the exported customization XML manually, since there’s no UI in Ms CRM 3.0 to edit filtering of customized view. And be warned, that this is unsupported way.

Try to export Contact entity from Customization, and find in any text editor “Associated View”, then find tag. Inside you will find:
<filter type="and">
<condition operator="eq" value="0" column="statecode">
</filter>

With this condition, you can apply to every Custom Entity to display only Active Record in associated view, by export, edit, import it back and don’t forget to publish the entity. By this mean, you can also put additional condition as needed. If you are not sure what is the format of the condition string, try to do it in the advanced find and see what the string looks like.

Tuesday, March 07, 2006

Sharepoint: Hide Toolbar for Anonymous User

In one of my projects, there's a requirement to change UI for Anonymous User. Sharepoint Services by default, display all the functionality to post new item, edit, etc, like in the List/Item Toolbar. And if user is not authorized, than there's screen to request access. This may be good for internal user, but if your Sharepoint is exposed to Internet for anonymous user. This feature looks weird. So, the workaround for this is to hide this toolbar.

What you need to do is editing the XML Schema/Template for each list. You can see list of existing templates in SPS at folder C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\template\1033\STS\LISTS. Below is example definition for announcement:

Javascript: showModal(less)Dialog

In Javascript, if you need to open new window using window.showModalDialog or window.showModelessDialog and then submit form inside the new window, then the form will be submitted to new window, which is usually not the way that we want it to be. There's one workaround that is easy to implement. Just put <base target=_self> inside <head> tag.

What is the difference between window.showModalDialog and window.showModelessDialog? From how Javascript call this two function, when window.showModalDialog is called, the code will stop there and wait until the new window is closed, but for window.showModelessDialog, the code will keep running and don't wait until new window is closed. So if your parent window is supposed to wait until result from new window returns, then use window.showModalDialog. Otherwise, If you don't care about the result or the result is not needed inside the calling method, then use window.showModelessDialog.

Preamble

Here I want to share to fellow developers what I found that is useful if you find the same case. Hope you can find this useful too. Happy to sharing with you. I have another blog that is not specific to programming, but because my life is around IT, so you will find it a lot about IT :). But hope I'm not categorized as nerd or geeky, but if I turn out to be.. then I just can laugh. Check gunady.blogspot.com.

Cheers,
Gunady