Formatting issues 
Author Message
 Formatting issues

This is from Forth Inc.'s benchmark page.

I'm curious about the rationale to format code in the way
shown here. Anybody care to explain?

It is clear by indentation where a new control construct
starts, but not where it ends. Personally, when reading
I always try to 'collapse' constructs by scanning for THEN
when seeing IF etc. That is very hard to do in this case.

The most curious part is allowing code after REPEAT and
putting THEN and LOOP on the same line.

It can not be explained by saying this is the original
Gilbreath Sieve, because that one didn't use '2*' AFAIR.

-- original --
8190 CONSTANT SIZE
CREATE FLAGS   SIZE ALLOT

: DO-PRIME ( -- n )
   FLAGS SIZE -1 FILL
   0 SIZE 0 DO

         I 2* 3 + DUP I +  BEGIN
            DUP SIZE < WHILE
               DUP FLAGS + 0 SWAP C! OVER +
         REPEAT  2DROP  1+
   THEN  LOOP ;

-marcel



Thu, 05 Feb 2004 14:26:22 GMT  
 Formatting issues
I guess the answer is 'semantics'

I too put code after a REPEAT when if factored out the
BEGIN ... REPEAT clause it would be gone together and
it's just a couple of words.

code_before
Begin ... Repeat Code_discussed
code_after

makes clearer the break between code_discussed and
code after.

But I'm more and more getting an 'horizontal' style, so
may be it's just a common degeneration.

Joan

Quote:

> This is from Forth Inc.'s benchmark page.

> I'm curious about the rationale to format code in the way
> shown here. Anybody care to explain?

> It is clear by indentation where a new control construct
> starts, but not where it ends. Personally, when reading
> I always try to 'collapse' constructs by scanning for THEN
> when seeing IF etc. That is very hard to do in this case.

> The most curious part is allowing code after REPEAT and
> putting THEN and LOOP on the same line.

> It can not be explained by saying this is the original
> Gilbreath Sieve, because that one didn't use '2*' AFAIR.

> -- original --
> 8190 CONSTANT SIZE
> CREATE FLAGS   SIZE ALLOT

> : DO-PRIME ( -- n )
>    FLAGS SIZE -1 FILL
>    0 SIZE 0 DO

>          I 2* 3 + DUP I +  BEGIN
>             DUP SIZE < WHILE
>                DUP FLAGS + 0 SWAP C! OVER +
>          REPEAT  2DROP  1+
>    THEN  LOOP ;

> -marcel



Thu, 05 Feb 2004 19:35:12 GMT  
 Formatting issues

Quote:

> The most curious part is allowing code after REPEAT and
> putting THEN and LOOP on the same line.

actually, we speak a bit about personal style - in some
cases it is beneficial to condense the code. As a general,
well, that's a bit too much probably.
a) THEN/LOOP/etc. on the same line: IYAM that is only
   allowed if their starting words happen to live on
   the same line too. That is often possible, i.e.

       ....
   THEN  LOOP
b) code after REPEAT is often okay, atleast for stack
   cleanup - and mostly there is similicar code just
   before BEGIN that did constitute the loop runvars.
   Again from this example:
   I 2* 3 + DUP I + BEGIN
      ....
   REPEAT 2DROP
c) that "1+" in the example will not bind to the
   while-loop - which might confuse the reader.

and to add a bit of my personal preference....

d) since loops are so important, I recommend to
   put a stack-comment after the loop-words
   (mostly the best place is after the opening word
    but sometimes not. Atleast one should be given).
   If the loop touches only part of the stackvalues
   then list only these, so we know the others are const.
e) put ONE additional space in between computations
   that are logically bound together (called
   expressions in computer languages being a bit
   more influenced from math writing).

which transforms the original into my style:

Quote:

> -- original --
> 8190 CONSTANT SIZE
> CREATE FLAGS   SIZE ALLOT

> : DO-PRIME ( -- n )
>    FLAGS SIZE -1 FILL
>    0 SIZE 0 DO

>          I 2* 3 + DUP I +  BEGIN
>             DUP SIZE < WHILE
>                DUP FLAGS + 0 SWAP C! OVER +
>          REPEAT  2DROP  1+
>    THEN  LOOP ;

  -- guidod --
  8190 CONSTANT SIZE
  CREATE FLAGS   SIZE ALLOT

  : DO-PRIME ( -- n )
     FLAGS SIZE -1 FILL
     0 SIZE 0 DO ( -- n' )

           I 2* 3 +  DUP I +  BEGIN ( ... step look )
              DUP SIZE < WHILE
                 DUP FLAGS +  0 SWAP C!  OVER +
           REPEAT  2DROP  
           1+
        THEN  
     LOOP ;

looking forward to see other's coding style ;-)
cheers,
-- guido                            Edel sei der Mensch, hilfreich und gut



Thu, 05 Feb 2004 19:59:39 GMT  
 Formatting issues

Quote:

> This is from Forth Inc.'s benchmark page.

> I'm curious about the rationale to format code in the way
> shown here. Anybody care to explain?

> It is clear by indentation where a new control construct
> starts, but not where it ends. Personally, when reading
> I always try to 'collapse' constructs by scanning for THEN
> when seeing IF etc. That is very hard to do in this case.

> The most curious part is allowing code after REPEAT and
> putting THEN and LOOP on the same line.

> It can not be explained by saying this is the original
> Gilbreath Sieve, because that one didn't use '2*' AFAIR.

> -- original --
> 8190 CONSTANT SIZE
> CREATE FLAGS   SIZE ALLOT

> : DO-PRIME ( -- n )
>    FLAGS SIZE -1 FILL
>    0 SIZE 0 DO

>          I 2* 3 + DUP I +  BEGIN
>             DUP SIZE < WHILE
>                DUP FLAGS + 0 SWAP C! OVER +
>          REPEAT  2DROP  1+
>    THEN  LOOP ;

> -marcel

I myself prefer a formatting style more like this

: DO-PRIME ( -- n )
   FLAGS SIZE -1 FILL
   0 SIZE 0 DO

      IF    I 2* 3 + DUP I +
            BEGIN   DUP SIZE <
            WHILE   DUP FLAGS + 0 SWAP C! OVER +
            REPEAT
            2DROP  1+
      THEN
   LOOP  ;

That is, when possible, IF ... ELSE ... THEN all begin
at the same indentation, BEGIN ... WHILE ... REPEAT are
all at the same level, and in BEGIN ... UNTIL I put the
final test at the indentation level of BEGIN and follow
with UNTIL, as in

        BEGIN  stuff
        test UNTIL

Can't imagine what the Forth, Inc. formatting style, if
not a typo, is intended to convey. Paper is cheap, white
space is cheap (nowadays!) so why not use it for clarity?

Of course a 38-word long definition should be a rara avis.
Word are much clearer if kept shorter. As we all know.

--
Julian V. Noble
Profesor of Physics

Galileo's Commandment:

   "Science knows only one commandment: contribute to science."
   -- Bertolt Brecht, "Galileo".



Thu, 05 Feb 2004 23:22:13 GMT  
 Formatting issues

Quote:
> looking forward to see other's coding style ;-)

8190 CONSTANT SIZE
CREATE FLAGS   SIZE ALLOT

: DO-PRIME ( -- N )
  FLAGS SIZE -1 FILL
  0 SIZE 0
  DO ( -- N' )

    IF
      I 2* 3 +  DUP I +
      BEGIN ( ... STEP LOOK )
        DUP SIZE <
      WHILE
        DUP FLAGS +  0 SWAP C!  OVER +
      REPEAT
      2DROP
      1+
    THEN
  LOOP
;

I like more of a "military" style where all the control structures (and
their equals) line up in columns :-)

Mike



Thu, 05 Feb 2004 23:40:53 GMT  
 Formatting issues



Quote:

> But I'm more and more getting an 'horizontal' style, so
> may be it's just a common degeneration.

    Fight it!

--

-GJC

-Abolish Public Schools.



Fri, 06 Feb 2004 01:35:05 GMT  
 Formatting issues

Quote:

> : DO-PRIME ( -- n )
>    FLAGS SIZE -1 FILL
>    0 SIZE 0 DO

>       IF    I 2* 3 + DUP I +
>             BEGIN   DUP SIZE <
>             WHILE   DUP FLAGS + 0 SWAP C! OVER +
>             REPEAT
>             2DROP  1+
>       THEN
>    LOOP  ;

> That is, when possible, IF ... ELSE ... THEN all begin
> at the same indentation,

I follow that style with the exception of when an
IF ... ELSE ... THEN  clause fits nicely on a
properly indented line and is still visible.

I do not indent 3 by default, 3 for LOOPs, 6 for IF,
and 8 for BEGIN.  I use indents of 2 or 3 consistenly.

Quote:
> BEGIN ... WHILE ... REPEAT are
> all at the same level, and in BEGIN ... UNTIL I put the
> final test at the indentation level of BEGIN and follow
> with UNTIL, as in

Sometimes I put a BEGIN clause on one line when it fits.

If I have lots of comments they usually follow a similar
indentation pattern to the code itself rather than a
straight column beyond the furthest right code.



Fri, 06 Feb 2004 01:39:37 GMT  
 Formatting issues


Quote:

> > looking forward to see other's coding style ;-)

> 8190 CONSTANT SIZE
> CREATE FLAGS   SIZE ALLOT

> : DO-PRIME ( -- N )
>   FLAGS SIZE -1 FILL
>   0 SIZE 0
>   DO ( -- N' )

>     IF
>       I 2* 3 +  DUP I +
>       BEGIN ( ... STEP LOOK )
>         DUP SIZE <
>       WHILE
>         DUP FLAGS +  0 SWAP C!  OVER +
>       REPEAT
>       2DROP
>       1+
>     THEN
>   LOOP
> ;

> I like more of a "military" style where all the control structures (and
> their equals) line up in columns :-)

    Nice.  It looks very much like my style!

    It would be a little nicer with more noticeable indenting (especially
when it might be viewed with a variable pitch font).

--

-GJC

-Abolish Public Schools.



Fri, 06 Feb 2004 01:40:40 GMT  
 Formatting issues

Quote:



> > > looking forward to see other's coding style ;-)

> > 8190 CONSTANT SIZE
> > CREATE FLAGS   SIZE ALLOT

> > : DO-PRIME ( -- N )
> >   FLAGS SIZE -1 FILL
> >   0 SIZE 0
> >   DO ( -- N' )

> >     IF
> >       I 2* 3 +  DUP I +
> >       BEGIN ( ... STEP LOOK )
> >         DUP SIZE <
> >       WHILE
> >         DUP FLAGS +  0 SWAP C!  OVER +
> >       REPEAT
> >       2DROP
> >       1+
> >     THEN
> >   LOOP
> > ;

> > I like more of a "military" style where all the control structures (and
> > their equals) line up in columns :-)

>     Nice.  It looks very much like my style!

>     It would be a little nicer with more noticeable indenting (especially
> when it might be viewed with a variable pitch font).

> --

> -GJC

> -Abolish Public Schools.

No code od any sort should be rendered in a variable pitch font unless
the intent is to obfuscate it. So there! I have written!

Jerry
--
Engineering is the art of making what you want from things you can get.
-----------------------------------------------------------------------



Fri, 06 Feb 2004 10:27:30 GMT  
 Formatting issues

Quote:

>> This is from Forth Inc.'s benchmark page.

>> I'm curious about the rationale to format code in the way
>> shown here. Anybody care to explain?

>> It is clear by indentation where a new control construct
>> starts, but not where it ends. Personally, when reading
>> I always try to 'collapse' constructs by scanning for THEN
>> when seeing IF etc. That is very hard to do in this case.

>> The most curious part is allowing code after REPEAT and
>> putting THEN and LOOP on the same line.

>> It can not be explained by saying this is the original
>> Gilbreath Sieve, because that one didn't use '2*' AFAIR.

>> -- original --
>> 8190 CONSTANT SIZE
>> CREATE FLAGS   SIZE ALLOT

>> : DO-PRIME ( -- n )
>>    FLAGS SIZE -1 FILL
>>    0 SIZE 0 DO

>>          I 2* 3 + DUP I +  BEGIN
>>             DUP SIZE < WHILE
>>                DUP FLAGS + 0 SWAP C! OVER +
>>          REPEAT  2DROP  1+
>>    THEN  LOOP ;

>> -marcel

>I myself prefer a formatting style more like this

>: DO-PRIME ( -- n )
>   FLAGS SIZE -1 FILL
>   0 SIZE 0 DO

>      IF    I 2* 3 + DUP I +
>            BEGIN   DUP SIZE <
>            WHILE   DUP FLAGS + 0 SWAP C! OVER +
>            REPEAT
>            2DROP  1+
>      THEN
>   LOOP  ;

>That is, when possible, IF ... ELSE ... THEN all begin
>at the same indentation, BEGIN ... WHILE ... REPEAT are
>all at the same level, and in BEGIN ... UNTIL I put the
>final test at the indentation level of BEGIN and follow
>with UNTIL, as in

>    BEGIN  stuff
>    test UNTIL

>Can't imagine what the Forth, Inc. formatting style, if
>not a typo, is intended to convey.

One thing it conveys is that they are Forth Inc. It conveys tradition, in
other words. Not a bad thing to convey if you're Forth Inc. Unnecessarily
and inappropriately speaking for Forth Inc., it may also ease going
low-level, i.e. in terms of what I imagine may occaisionally face Forth
Inc., going back to BLOCKS on a target system that was developed with a
nurse system.

Rick Hohensee
                                                www.clienux.com

Quote:
>Paper is cheap, white
>space is cheap (nowadays!) so why not use it for clarity?

>Of course a 38-word long definition should be a rara avis.
>Word are much clearer if kept shorter. As we all know.

Perhaps open airy definitions in BLOCKable formatting most typifies Forth
these days.

Much of my code lately has been very assembler-like in terms of
flow-control. H3sm has it's own flow-control constructs, but they are
basically assembly with a flag (low byte of TOS), a branch (ell, yes, no),
and a small set of standard branch targets per definition. I've also at
this point written a fair bit of actual x86 assembly (H3sm itself), and an
assembler in Bash, some of which actually works :o). I find unrestricted
use of whitespace to be hugely helpful in all cases.

I also have been adhering to "two blank lines between definitions" in most
things, although H3sm source retains the boundary marker lines between
routines that Michael Somos originally added, in part because those
boundaries both in assembly and in the preceding C version were not
explicit in the code otherwise. H3sm is all in main() in C.

There seems to be a belief generally that low-level code has to be ugly.
It seems to me that the value of things like proper naming and so on goes
through the roof the closer you get to the metal. Imagine x86 assembly in
which comments are completely unnecessary. Forth itself is an exercise in
using the best coding techniques possible at the lowest level possible.

Rick Hohensee
                                                        www.clienux.com

- Show quoted text -

Quote:

>--
>Julian V. Noble
>Profesor of Physics

>Galileo's Commandment:

>   "Science knows only one commandment: contribute to science."
>   -- Bertolt Brecht, "Galileo".



Fri, 06 Feb 2004 11:47:37 GMT  
 Formatting issues


Quote:

> No code od any sort should be rendered in a variable pitch font unless
> the intent is to obfuscate it. So there! I have written!

    Ok, I guess I'll have to throw away all of my web browsers then.

--

-GJC

-Abolish Public Schools.



Fri, 06 Feb 2004 12:46:27 GMT  
 Formatting issues

[ view code ]

Quote:
>     Ok, I guess I'll have to throw away all of my web browsers then.

Don't want to discourage that, but it will suffice to set your preferred
font for quoted material to non-proportional.

-marcel



Fri, 06 Feb 2004 13:17:49 GMT  
 Formatting issues

Quote:



>> No code od any sort should be rendered in a variable pitch font unless
>> the intent is to obfuscate it. So there! I have written!
>     Ok, I guess I'll have to throw away all of my web browsers then.

If your web browser renders code in a variable pitch font then your
web browser deserves to be thrown away.

Andrew.



Fri, 06 Feb 2004 15:35:49 GMT  
 Formatting issues


Quote:


> [ view code ]

> >     Ok, I guess I'll have to throw away all of my web browsers then.

> Don't want to discourage that, but it will suffice to set your preferred
> font for quoted material to non-proportional.

    And then it won't be right something else.

--

-GJC

-Abolish Public Schools.



Fri, 06 Feb 2004 16:28:03 GMT  
 Formatting issues

Quote:



>> No code od any sort should be rendered in a variable pitch font unless
>> the intent is to obfuscate it. So there! I have written!

>    Ok, I guess I'll have to throw away all of my web browsers then.

Yeah. Get Lynx :o)

Rick Hohensee
                                        ww.clienux.com

Quote:

>--

>-GJC

>-Abolish Public Schools.



Fri, 06 Feb 2004 16:40:14 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Physical formatting issues

2. expr / clock format issue

3. CFP: The AI Pride Issue (IEEE intelligent systems special issue)

4. routine to convert date format 0CYYDDDF to format DD/MM/YY or DDMMYY

5. Format of a TSO XMIT format dataset

6. Joaquin's formatted messages and the end of formatting

7. format specifiers in format string param.

8. More format bugs (was format(1/2,1,1,,0)

9. conversion of .dxf format to vrml 2.0 format

10. translate code in TASM format to MASM format

11. MSBIN float format <-> IEEE float format

12. translate code in TASM format to MASM format

 

 
Powered by phpBB® Forum Software