Re: Factoring - a newbie question
- From: Elizabeth D Rather <eratherXXX@xxxxxxxxx>
- Date: Sun, 01 Jul 2007 12:03:16 -1000
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."
==================================================
.
- Follow-Ups:
- Re: Factoring - a newbie question
- From: Robert Miller
- Re: Factoring - a newbie question
- References:
- Factoring - a newbie question
- From: Robert Miller
- Factoring - a newbie question
- Prev by Date: Re: class sort
- Next by Date: Re: class sort
- Previous by thread: Re: Factoring - a newbie question
- Next by thread: Re: Factoring - a newbie question
- Index(es):
Relevant Pages
|