Saturday, October 28, 2006
Refactoring: Replace Method with Method Object
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
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
<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
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
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
Cheers,
Gunady