[REBOL] source code layout question
From: philb:upnaway at: 5-Jan-2002 9:12
Hi Joel,
Well for good or bad I code pretty much in style B in all the langauges I use
C, C++, Progress, Rebol.
I just like my braces or square brackets lined up !!
Everything inside the brackets indented of course, unless it fits on a single line in
which case save some space by having the close bracket on the same line.
foo: func [x [integer!] y [integer!] z [integer!]]
[
either x = 0
[z]
[
either y = 0
[head reverse z]
[
while [x < y]
[
y: y - x
append z "x"
]
foo y x z
]
]
]
of course everyone will have their own variation on one of the styles.
Cheers Phil
=== Original Message ===
Hi, all,
WARNING #1: None of the code in this post is meaningful, and may
not even be legitimate. It's only included for
the purpose of illustration.
WARNING #2: This is a controversial and sensitive subject with
some folks. I'm not trying to start a flame war, and
I have not interest in debating aesthetic opinions. I *am* very
interested in objective criteria which identify strengths and
weaknesses of the alternatives.
That said...
I've recently been pondering source code layout. I, like many of
us on the list, have been writing REBOL in a style similar to the
following, which I'll call Style B:
foo: func [x [integer!] y [integer!] z [integer!]] [
either x = 0 [
z
][
either y = 0 [
head reverse z
][
while [x < y] [
y: y - x
append z "x"
]
foo y x z
]
]
]
This style is fairly conventional for block-structured languages
(i.e. descendants of Algol, including Pascal, c, Java, Perl, etc.)
Some programmers use a variation on this style, which I'll call
Style C:
foo: func [x [integer!] y [integer!] z [integer!]]
[
either x = 0
[z]
[
either y = 0
[head reverse z]
[
while [x < y]
[
y: y - x
append z "x"
]
foo y x z
]
]
]
LISP (and Scheme) programmers often use a bracketing style which
emphasizes the indentation of actual content, and relegates the
punctuation to a minor role. This approach would give us a
different look, which I'll call Style L:
foo: func [x [integer!] y [integer!] z [integer!]]
[either x = 0
[z]
[either y = 0
[head reverse z]
[while [x < y]
[y: y - x
append z "x"]
foo y x z]]]
REBOL isn't Algol, Lisp, or c (etc...), so perhaps its style is
not subject to the constraints of those languages. Here's my
assessment of some of the strengths and weaknesses of these
styles. (Again, I'm trying to deal with measurable features
and human factors, regardless of what I personally like or
dislike!) As all of these styles use indentation of source
text to show nesting, that issue is irrelevant to a contrast.
Style Pros Cons
----- ------------------------- -------------------------
B Familiar, even to people Deeply-nested code can
used to other common require lots of lines that
programming languages. only close blocks ("]").
Can be cut-and-pasted into May obscure distinctive
REBOL console, since open REBOL concepts (e.g., the
blocks signal that the brackets commonly written
current expression is not around WHILE arguments are
yet complete. not "required syntax" as
with c parentheses and/or
braces.
Easy to insert/delete lines
within a multi-line block
while revising code.
C Ease of insertion/deletion Requires more lines than
as with Style B. other styles to express the
same code.
L Keeps short blocks on the Long runs of "]" at the end
same line, thus saving of multiple nesting can be
vertical layout space. difficult to count (and get
correct.
Inserting/deleting block
content can require more
editing work.
I'm interested in any other comments on the objective pros/cons
of these styles.
For the sake of experiment, I've been experimenting with a hybrid
style, which I'll call Style R. It is intended to meet the
following criteria:
* Use indentation to make nesting visible (as do B, C, and L).
* Emphasize the idea of block-as-value (in contrast with the
keywords-with-required-punctuation implication of B and C).
* Avoid hard-to-read runs of punctuation (as in L).
* As far as possible, subordinate to the other criteria, use
as few lines as possible (to get the maximum code on a page
or within a window).
Guided by these criteria, the current state of Style R would
lay out the above sample code this way:
foo: func [x [integer!] y [integer!] z [integer!]]
[ either x = 0
[ z]
[ either y = 0
[ head reverse z]
[ while [x < y]
[ y: y - x
append z "x"
]
foo y x z
] ] ]
If one wanted to be polite and include help strings, the code
could be laid out as follows:
foo: func
[ "Computes a Euclid string for numeric data"
x [integer!] "major argument"
y [integer!] "corporal argument"
z [integer!] "seed string, modified by evaluation"
][ either x = 0
[ z]
[ either y = 0
[ head reverse z]
[ while [x < y]
[ y: y - x
append z "x"
]
foo y x z
] ] ]
Objective comments (i.e., something other that "That's the
ugliest thing I've ever seen!" or "Wow! You're an artist!" ;-)
are welcome!
-jn-
--
; sub REBOL {}; sub head ($) {@_[0]}
REBOL []
# despam: func [e] [replace replace/all e ":" "." "#" "@"]
; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"}
print head reverse despam "moc:xedef#yleen:leoj" ;