Search DominoPower's 11,441 Lotus-related article archive 
Home
EasyPrint
News details Click here for the RSS feed's XML code. This is not a browser URL.
Articles-only Click here for the RSS feed's XML code. This is not a browser URL.
Twitter Feed Click here for the Twitter feed.
Using recursion in LotusScript (continued)

It's a little more complicated than our factorial example, but there are only six lines of code that require any real thought. Let's look at that If statement:

If Not Instr(currstring, replace) = 0 Then

The If statement is where we determine whether or not we've arrived at the end case. Are there any more back slashes present? If so, we launch into the recursive case:

lefttext = Left(currstring, Instr(currstring, replace)-1)
righttext = Right(currstring, Len(currstring) - (Instr(currstring, replace) - 1 + Len(replace)))
fulltext = lefttext + replacewith + righttext
replacesubstring = replacesubstring(fulltext, replace, replacewith)

The first three lines merely reassemble the string. Everything to the left of the text we want to replace is added to the text we want to replace it with. This string is then added to everything to the right. (If we were trying to replace back slashes with forward slashes in "Demo\TestDB.nsf", we'd add "Demo" to "/" and then add that to "TestDB.nsf".)

The fourth line in the recursive case is what truly makes this function recursive. We take the string we've just reassembled and check for more backslashes by recursively calling the ReplaceSubstring function.

The remainder of the important code consists entirely of:

replacesubstring = currstring

This is our end case, where we've finally reduced the problem to a point where it can be easily solved.

Drawbacks to using recursion
While recursion can be a powerful tool, that power does come with drawbacks. The first drawback is that it takes some practice before you can start slamming out recursive code. If you're in a hurry to meet a deadline, you'll almost certainly use the Evaluate statement in LotusScript before you'd attempt to rewrite @ReplaceSubstring as a recursive function.

Another drawback is that recursive code may confuse those who aren't already familiar with recursion. Most Domino developers can muddle through someone else's LotusScript without too many problems (especially if it's well commented!). But looking at someone else's recursive code for the first time can be quite a trial.

Perhaps the biggest drawback to using recursion is the limit that Lotus Notes imposes on recursive functions. If your elegantly written recursive function needs to call itself too many times, you'll receive an "Out of Stack Space" error. This is because Lotus Notes has a 32K limit on the amount of information it stores in the stack for recursive functions. In other words, Lotus Notes won't be able to keep track of all of the temporary variables your function is using!

The first two drawbacks can be easily overcome, but that 32K-stack limit can be a real problem. Luckily, you can usually give yourself a little more stack space to play with by making fewer recursive calls. For instance, if we were to check for two back slash characters each time through our ReplaceSubstring function, we'd cut the number of recursive calls in half.


« Previous  ·  1  ·  2  ·  3  ·  4  ·  Next »
Other articles you might like
Home > Lotus Technologies > LotusScript (64 articles)
   When the debugger won't debug hidden code that isn't hidden
   What to do if the LotusScript debugger won't single-step over code
   Little known traps about Lotus Notes fields
Get Weekly Email Updates
Subscribe to our regular weekly email newsletter. It's packed with tips, reviews, deep analysis, and the latest news.
 
Recent DominoPower Articles
Application development, William Shatner, and the origin of the universe
Learn Domino Designer 8.5 for free
The (near) future of Sametime, Quickr, Connections, and Symphony
Inside the IBM Innovations lab
Lotusphere 2010: Hot fixes and cool news for Notes, Domino, and LotusLive
Lotusphere 2010: mobility and collaboration
2010: A Lotusphere of change
Latest Lotus Headlines
Xpages not loading? JVM errors? - Solution
How to implement an iCalendar feed into your Notes calendar with XPages
DWA Hotfixes for Domino 8.5.1FP1 - A Gotcha
IBM Adds DB2 to Lotus Foundations SMB Package
SNTT : XPages onclick Ghosts in the machine
Ports used by Lotus Sametime 8.5 servers
Exploring a Domino Date Bug
>> Read all the news
More from the ZATZ journals
Computing Unplugged: The iPad defenders have spoken
David Gewirtz Online: CNN commentary and analysis
OutlookPower: More about disappearing text
-- Advertisement --

Sophisticated Meets Simple For Document Management
Share. Control. Manage.
Documents, emails, and content in the context of how work is done. Native to Lotus Domino. The User Experience unseen for Lotus Domino. Do more with less. Really.

See the possibilities Docova unleashes for Lotus Domino.
-- Advertisement --

Struggling with exporting Notes data to spreadsheets? No More!
Try IntelliPRINT, The world's leading Reporting, Dashboards, and Analysis solution for Notes & Domino

  • Don't spend unproductive time maintaining different versions of the same spreadsheet
  • Preserve data integrity and security in multi-user environments
  • Create reports in minutes INSIDE Notes
  • Get freedom from iterative report requests, deliver self-serve capabilities

Experience Reporting, Dashboards, and Analysis INSIDE Notes.

Try IntelliPRINT NOW!

ZATZ Home  ·  News  ·  Back Issues  ·  Credits/Trademarks ·  Link To Us
Copyright © 1998-2010, ZATZ Publishing. All rights reserved worldwide.
Editor's Login