Hello,

One day, not long ago, slava fixed a bug in the golden-section demo. Like 
modern day politicians that use a bill as a trojan horse to fast-track agenda 
items, the bug fix was a payload to remove a variable and make everything use 
the stack. :-) Looking back, it's kind of funny. Nice work senator Pestov. :-)

Now let me explain why the changes were bad from a design standpoint. I'd like 
to follow an MVC model as much as possible in my code. golden-section.factor 
has the following layout:

        UTILITY WORDS

        !!!!!!!!!!

        MODEL

        !!!!!!!!!!

        SETUP

The MODEL portion is pure logic. The model portion is easily abstracted from 
any drawing technology. I mistakenly claimed on the channel that the model 
was free from openglisms. Actually, there are two calls to glColor4f. But no 
matter what drawing technology the VIEW uses, you would have to set the color 
in those places. So I could have an abstracted color setting word where 
glColor4f is called.

This is the original model:

: phi ( -- phi ) 5 sqrt 1 + 2 / 1 - ;

: omega ( i -- omega ) phi * 2 * pi * ;

: x ( i -- x ) dup omega cos * 0.5 * ;

: y ( i -- y ) dup omega sin * 0.5 * ;

: center ( i -- point ) dup x swap y 2array ;

: radius ( i -- radius ) pi * 720 / sin 10 * ;

: color ( i -- color ) 360.0 / dup 0.25 1 4array ;

: rim ( i -- ) black first4 glColor4f dup center swap radius 1.5 * disk ;

: inner ( i -- ) dup color first4 glColor4f dup center swap radius disk ;

: dot ( i -- ) dup rim inner ;

: golden-section ( -- ) 720 [ dot ] each ;

Slava fixed the bug but changed the model:

        : rim ( quadric i -- )
        black first4 glColor4f dup radius 1.5 * swap center disk ;

        : inner ( quadric i -- )
        dup color first4 glColor4f dup radius swap center disk ;

        : dot ( quadric i -- ) 2dup rim inner ;

        : golden-section ( quadric -- ) 720 [ dot ] each-with ;

At least he was considerate in not indenting my code. ;-)

So now those four words accept a 'quadric' on the stack.

Quadric has no analogy in raster graphics libraries. Having it in the stack 
effect of words that are part of the model makes the model incompatible with 
possible backends such as Yuuki's web ui or pure xlib for examples.

I add a parameter to a function (or a word) whenever there's a piece of the 
algorithm I'd like to change often. Quadric is not something I'd like to 
change often. The OpenGL redbook advises that quadrics be reused when 
possible. So I designed a 'disk' word for use in golden-section. The disk 
word reuses a quadric that is stored in a 'quadric' variable. The design 
matched the advice on how to use quadrics.

I really like how enthusiastic some of the Factor community is about doing 
pure stack programming. I'm not kidding when I say this: I really really 
really want folks to dig into the guts of Factory and the demos and rewrite 
things in a way that only uses the stack. I think it's a good exercise. In 
fact, I do this myself. For example, I rewrote cabal.factor using only the 
stack and it came out pretty nice! In my experience, variable based code is 
easier to develop and change. Once the design crystalizes, I usually try and 
change things to only use a stack. I've rewriten parts of Factory this way.

Ed

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to