We are starting a new series of blog posts, where we are trying to share the experience of our developers from coding perspective. These are problems that our developers encountered while coding and how they solved it. We are rightly calling it “Monday Blues”.
Having successfully created the XPages version for a critical application for our customer, I was feeling terrific as we were ahead of schedule. Patting myself on the shoulder, I thought that I could give myself a well deserved rest and go out and watch the cricket match (know more about Cricket here. It’s like a national game for Indians and we won the world championship last year). Just as I was about to leave, the test lead brought me down to earth with a bang. He raised 15 bugs and stated that my counts and calculated values were all wrong!!!!. Having painstakingly created the fields and checked and rechecked for the last five days, I was amazed that there could be so much of difference in the calculations. So I rolled up my sleeves, removed Cricket from my mind and sat in total concentration for the next two hours, looking at code for calculations which seemed perfectly correct , yet gave wrong answers. It took quite some time for me to get at the fact that the problem lay in the variable declaration!!
If you declare any variable in X-pages the scope is available to the entire X-page! You cannot destroy the variable!! Having used 500+ variables in my application, there were 3 or 4 variables which had got re-declared!
Let us look at a sample of what may have happened :
Assume I have three Computed fields in my XPages : fldComp1, fldComp2 and fldComp3
Assume that the Server Side Java Script code for the fields are given as
var EstimatedPrice =12;
var Profitmargin =8;
At runtime, fldComp1 gets a result of 20
At runtime , Expected result: 0
(EstimatedPrice is declared as 20 and EstimatedTotal gets a return value of 20 by using the getComponent function)
whereas Actual result :8
This happens because EstimatedPrice is declared as 20 for fldComp2, but when getComponent function is called, EstimatedPrice is reset as 12 (as this variable also exists in fldComp1)
Now, EstimatedTotal has got the return value of 20; EstimatedPrice has the value 12
so,here EstimatedTotal – EstimatedPrice —-> 20-12 = 8
i.e fldComp2 has a computed value of 8
var EstimatedUnits = 10;
return EstimatedUnits * EstimatedTotal ;
At runtime, Expected Result: Error
since EstimatedTotal is not declared in the code for the field fldComp3 but Actual Result : 200
since it will take the value which was calculated for EstimatedTotal from fldComp2 and multiply it with EstimatedUnits which is 10.
You might have the same problem with the 83rd field that you declared and the 637th field in the Xpage. In such a scenario, you could get bogged down and spend hours trying to zero in on the actual problem.
- As far as possible, especially for big applications, minimize using code in X-pages and make maximum use of the script library. The scope of the variable in a script library is limited to the function only.
- In smaller applications ,if you have used code in the X-Page itself, follow a Naming convention for variables such that repetition of variable names is avoided at all costs.