Re: FizzBuzz



On Mar 9, 8:49 pm, "Doug Hoffman" <dhoff...@xxxxxxxxxxxxxxx> wrote:
On Mar 9, 8:34 pm, "J Thomas" <jethom...@xxxxxxxxx> wrote:

I like most of the solutions that have been presented. But if you want
the ultimate of maintainability, something like this is likely to be
good:

10 FOR I = 1 TO 100
20 IF (I MOD 3 = 0) THEN PRINT "Fizz";
30 IF (I MOD 5 = 0) THEN PRINT "Buzz";
40 IF ((I MOD 3 <> 0) AND (I MOD 5 <> 0)) THEN PRINT I;
50 PRINT
60 NEXT I

I don't really think that BASIC is easier to learn than Forth. But a
lot of people think it is, and a lot of people learn it very quickly
-- maybe partly because their confidence is so high. Practically
anybody can quickly learn enough BASIC to maintain this program.

You didn't solve the problem. The problem was clearly defined as
FizzBuzzBoomZapp. From 1 to 1000. Read up a few posts if you didn't
catch that. (Hint: It might help to use Forth. I'm fairly certain
this is a Forth newsgroup.)

Have I done something that offended you? I think I'm detecting a
certain coolness to your tone. I remember years when this newsgroup
was nice and mellow, experts and hobbyists and newbies got along real
well. And only a few years ago the only real irritants were Jeff Fox
and John Passaniti. Oh well.

I like your solution to the 4 variable problem. If there's any
improvement to be made it would be factoring your 4 subroutines, which
repeat the exact same code 4 times and change only the data.

Here's one way:

: makesub ( n -- ) ( E: i -- 0|n )
CREATE C, PARSE-NAME STRING,
DOES> TUCK C@ MOD TUCK 0= IF 1+ COUNT TYPE 0 THEN DROP ;

3 makesub fizz? Fizz
5 makesub buzz? Buzz
7 makesub boom? Boom
9 makesub zapp? Zapp

I find this code harder to read, but I'm guessing it might be slightly
more maintainable since instead of copying and pasting the definition
you could just do

11 makesub zilch? Zilch

etc.

On the other hand, if you're going to have a whole series of them, why
name them? You could put the addresses of the data structures into an
array, and loop through the array. But this is overkill.

I think this is somewhat less readable. But the code should be
smaller. We re-use the same code 4 times. When should we sacrifice
readability for size? On memory-limited systems, one of Forth's
traditional niches?


Now, the bit-array approach is very nice but it depends on 3 5 *
fitting into a cell, and 3 5 * 7 * does not fit except in a few
systems. We can do without the double, triple, and quadruple bit-
arrays. I'll use a similar method that I think is more readable.

in place of .... 3 MOD DUP 0= IF ." Fizz" THEN

we can use

..... 1 fizzle DUP 2@ = IF ! ." Fizz" FALSE EXIT THEN +! TRUE ....

We count to three, when we get there we reset the counter.

One advantage of this approach is that it can be maintained by
somebody who isn't real clear what MOD does. And it's faster when MOD
is slow compared to this long expression.

Anyway, I liked your solution. And if you want readability, nothing
can beat the BASIC version which I won't bother to modify for you.

.



Relevant Pages

  • Re: Code Behind vs not
    ... maintainability are certainly high priority, ... Because truly efficent code in NEVER readable, ... > understanding is a result of readability. ... > long time delphi programmer, I prefer VS to borland in this arena). ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Code Behind vs not
    ... > maintainability are certainly high priority, ... >> The best code is both readable and maintainable and balances efficency. ... >> understanding is a result of readability. ... >> long time delphi programmer, I prefer VS to borland in this arena). ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Highly used programs: any better replacements out there?
    ... The maintainability issue has been made very clear (well, ... But where is the readability concern/advantage? ... For IBM-MAIN subscribe / signoff / archive access instructions, ... send email to listserv@xxxxxxxxxxx with the message: GET IBM-MAIN INFO ...
    (bit.listserv.ibm-main)
  • Re: Dim a byte array.
    ... > coding techniques across the years. ... the first code I posted was in response to Randy saying it ... As to maintainability or even readability... ... > maintainability were apparently not high on his list of concerns.... ...
    (microsoft.public.vb.general.discussion)
  • Re: yield_all needed in Python
    ... I don't see a lot of difference in readability and maintainability ... If we're talking about machinery behind the scenes to shortcut chains of ... nearly constant time in most situations. ...
    (comp.lang.python)