Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #47375

Re: How could have I factored this better?

Subject Re: How could have I factored this better?
References <5e60dbda-f34b-4be0-bfac-d32ddc67062f@googlegroups.com> <871t4we4e3.fsf@jester.gateway.pace.com>
Newsgroups comp.lang.forth
From "Elizabeth D. Rather" <erather@forth.com>
Organization FORTH, Inc.
Date 2016-05-20 14:38 -1000
Message-ID <ytydnUY4KviMM6LKnZ2dnUU7-R_NnZ2d@supernews.com> (permalink)

Show all headers | View raw


On 5/20/16 12:44 PM, Paul Rubin wrote:
> Darin Murphy <murphy.darin@gmail.com> writes:
>> Hi everybody. I created the following program and would like to know
>> how I could have factored and documented it better.
>
> Here's my version, which possibly uses some gforth words:
>
>     : checkneg ( n -- ) 0 < abort" need a non-negative integer" ;
>     : star  ( -- ) '*' emit ;
>     : stars ( n -- ) dup checkneg 0 ?do star loop ;
>     : solid-box-stars { cols rows } cr
>         cols checkneg
>         rows checkneg
>         rows 0 ?do
>             cols stars cr
>         loop ;
>
>
> Differences from yours:
>
> 1. The stack comment in your STAR word is ( n -- ) but should be ( -- )
> 2. I used ABORT to break out of the error check since the number
>    is invalid and there's no way to continue.  In a bigger program
>    you might want to catch the error with CATCH.
> 3. I checked for < 0 instead of < 1, since it seems perfectly ok to
>    me to print zero stars without it being an error.  That meant
>    changing the loops to ?DO.  The repeat/while stuff was clumsy.
> 4. I used gforth locals for the args to solid-box-stars.  Maybe
>    that's controversial but it avoids some stack juggling, assuming
>    you want the interface to stay what you wrote.
>

I don't see that the locals contributed anything. In fact, this is 
shorter, and avoids the need for code to manage the locals frame.

     : checkneg ( n -- ) 0 < abort" need a non-negative integer" ;
     : star  ( -- ) '*' emit ;
     : stars ( n -- ) dup checkneg 0 ?do star loop ;
     : solid-box-stars ( cols rows ) cr
         dup checkneg 0 ?do
             dup stars cr
         loop drop ;

In a real-world application, you should know where your arguments are 
coming from, and whether it's possible that one may be illegal. Often 
they're not; for example, if the boundaries of your box are specified in 
variables somewhere, the appropriate place to check would be where the 
variables are filled; thereafter, you know you have legal parameters.

Cheers,
Elizabeth

-- 
==================================================
Elizabeth D. Rather   (US & Canada)   800-55-FORTH
FORTH Inc.                         +1 310.999.6784
5959 West Century Blvd. Suite 700
Los Angeles, CA 90045
http://www.forth.com

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

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How could have I factored this better? Darin Murphy <murphy.darin@gmail.com> - 2016-05-20 13:09 -0700
  Re: How could have I factored this better? rickman <gnuarm@gmail.com> - 2016-05-20 16:18 -0400
    Re: How could have I factored this better? "Elizabeth D. Rather" <erather@forth.com> - 2016-05-20 10:25 -1000
      Re: How could have I factored this better? Darin Murphy <murphy.darin@gmail.com> - 2016-05-20 13:39 -0700
    Re: How could have I factored this better? Darin Murphy <murphy.darin@gmail.com> - 2016-05-20 21:33 -0400
  Re: How could have I factored this better? "Elizabeth D. Rather" <erather@forth.com> - 2016-05-20 10:23 -1000
  Re: How could have I factored this better? Paul Rubin <no.email@nospam.invalid> - 2016-05-20 15:44 -0700
    Re: How could have I factored this better? "Elizabeth D. Rather" <erather@forth.com> - 2016-05-20 14:38 -1000
      Re: How could have I factored this better? Paul Rubin <no.email@nospam.invalid> - 2016-05-20 17:49 -0700
        Re: How could have I factored this better? "Elizabeth D. Rather" <erather@forth.com> - 2016-05-20 14:57 -1000
          Re: How could have I factored this better? Paul Rubin <no.email@nospam.invalid> - 2016-05-20 18:34 -0700
            Re: How could have I factored this better? "Elizabeth D. Rather" <erather@forth.com> - 2016-05-20 16:00 -1000
              Re: How could have I factored this better? Paul Rubin <no.email@nospam.invalid> - 2016-05-20 20:08 -0700
                Re: How could have I factored this better? "Elizabeth D. Rather" <erather@forth.com> - 2016-05-20 17:24 -1000
                Re: How could have I factored this better? hughaguilar96@gmail.com - 2016-05-21 18:15 -0700
            Re: How could have I factored this better? Albert van der Horst <albert@cherry.spenarnc.xs4all.nl> - 2016-05-21 10:01 +0000
          Re: How could have I factored this better? Darin Murphy <murphy.darin@gmail.com> - 2016-05-20 22:15 -0400
            Re: How could have I factored this better? "Elizabeth D. Rather" <erather@forth.com> - 2016-05-20 16:46 -1000
            Re: How could have I factored this better? hughaguilar96@gmail.com - 2016-05-21 18:36 -0700
              Re: How could have I factored this better? Darin Murphy <murphy.darin@gmail.com> - 2016-05-21 22:51 -0400
                Re: How could have I factored this better? hughaguilar96@gmail.com - 2016-05-21 22:23 -0700
      Re: How could have I factored this better? Darin Murphy <murphy.darin@gmail.com> - 2016-05-20 22:43 -0400
    Re: How could have I factored this better? Darin Murphy <murphy.darin@gmail.com> - 2016-05-20 22:19 -0400
  Re: How could have I factored this better? "WJ" <w_a_x_man@yahoo.com> - 2016-05-21 22:02 +0000
    Re: How could have I factored this better? Darin Murphy <murphy.darin@gmail.com> - 2016-05-21 22:30 -0400
      Re: How could have I factored this better? humptydumpty <ouatubi@gmail.com> - 2016-05-21 19:40 -0700
  Re: How could have I factored this better? humptydumpty <ouatubi@gmail.com> - 2016-05-21 19:38 -0700
    Re: How could have I factored this better? Darin Murphy <murphy.darin@gmail.com> - 2016-05-21 23:06 -0400
      Re: How could have I factored this better? Paul Rubin <no.email@nospam.invalid> - 2016-05-21 20:59 -0700
        Re: How could have I factored this better? Darin Murphy <murphy.darin@gmail.com> - 2016-05-22 03:30 -0400
          Re: How could have I factored this better? "Elizabeth D. Rather" <erather@forth.com> - 2016-05-21 23:14 -1000
  Re: How could have I factored this better? humptydumpty <ouatubi@gmail.com> - 2016-05-27 03:54 -0700

csiph-web