Re: Factoring - a newbie question



Robert Miller wrote:
Is there any rule of thumb about when to factor lengthy words?

I'm thinking in terms of the added overhead of calling the new word and the additional effort versus the shorter ( and presumably more readable) definition and the relative ease of thoroughly testing the smaller factors. I've done (am doing) a bit of (so-far) productive factoring on a very complex word and am wondering where to draw the line

To make a really intelligent decision about these tradeoffs you need to know what they are, and that depends on the implementation you're using. On FORTH, Inc.'s MSP430 cross-compiler, the cost of calling a new word is just the call/return instructions. Indeed, most Forths are optimized to minimize this cost, because of the extreme modularity that characterized Forth programming style. So, check your documentation and/or look at what's actually compiled in individual words for entering and leaving them.

I have a word consisting of 27 words in which a 7 word sequence appears twice. Factoring this would save 12 words and assuming I can think of a good name for the sequence, the end result should be more readable.

The issue is often less whether factoring the sequence would save or cost and more whether the factor is a nameable concept (as opposed to a sequence of instructions that happens to match). Also, look at your whole application: are there similar sequences elsewhere? Can this concept be useful in other places?

The target processor is a TI MSP430 and in this instance neither speed nor space are important issues. Once the word is tested, readability matters only if I (or someone else) have to revisit this particular word - which I think is unlikely, but one never knows.

If neither speed nor space are issues, then the important tradeoffs center around testability and maintainability. You almost certainly *will* have to revisit this word; studies indicate that over the whole life of an application a significant amount of its cost is in code maintenance.

Look for factors that have a functional integrity that suggests a name, define them, test them, and use them wherever appropriate. This will generally result in more reliable code (because short definitions are easier to test thoroughly, and re-using tested code improves the reliability of the whole application). If you use good names, it will also result in improved readability. It will almost certainly make your programs smaller, and the performance cost is probably negligible. So, it's a win all the way around.

Of course, if your application is all finished now, as Jonah points out, re-factoring it introduces an element of risk. However, as you describe yourself as a newbie, I think it would be worthwhile. What you need to be doing is training your mind to spot factorable elements in advance, so that you write your programs in tiny elements from the beginning. But that takes practice: here's a good opportunity to get some.

Cheers,
Elizabeth

--
==================================================
Elizabeth D. Rather (US & Canada) 800-55-FORTH
FORTH Inc. +1 310-491-3356
5155 W. Rosecrans Ave. #1018 Fax: +1 310-978-9454
Hawthorne, CA 90250
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================
.



Relevant Pages

  • Re: Best Match sort sequence
    ... I've tried Internet Explorer, Firefox, Netscape and Safari. ... seller status with good star ratings, high item cost, lots of existing ... Things that will count against you include postal cost being higher ... The sequence is different depending on which browser is used. ...
    (uk.people.consumers.ebay)
  • Re: Best Match sort sequence
    ... results are nothing like each other, which leads me to believe that a seller ... requiring Paypal payment at time of purchase. ... Things that will count against you include postal cost being higher ... The sequence is different depending on which browser is used. ...
    (uk.people.consumers.ebay)
  • Re: Attn: Atheists & Skeptics - Whats wrong with answersingenesis.com?
    ... >> it would take to sequence one set of DNA and change the base pairs to ... >>> John Drayton ... They wanted to get the cost ... By the time the chicken genome was sequenced ...
    (talk.origins)
  • Re: use of base.
    ... I think there is a cost to them -- extra code and readability. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: optimizing simple-arrays
    ... Here's another approach where one first builds a sequence of the wrong ... note: doing float to pointer coercion (cost 13), ...
    (comp.lang.lisp)