counter++ versus "add 1 to counter"

Mark Wieder mwieder at ahsoftware.net
Sun Mar 21 12:39:22 EST 2004


A.C.T.-

(adding to what Scott Rossi said...)

Sunday, March 21, 2004, 7:45:00 AM, you wrote:

ACT> Actually, it's not clearer: One of the first programming languages I
ACT> learned was Machine Language (not Assembler but the hex codes needed to
ACT> make something happen). There were codes that did "incremet" the content
ACT> of a register - which is exactly what "counter++" does: Increment a
ACT> value (by one). That's clear and there's no doubt about what is meant.

Not at all. Look at the difference in C between counter++ and
++counter. What would you expect to get from

printf("%s", ++counter + someValue);
vs
printf("%s", counter++ + someValue);

Clarity starts to get muddied here, even though as you point out,
we're still dealing with simple incrementing. It takes a bit of
thinking to remember whether postincrementing happens before or after
the printf execution.

In comparison, Transcript's

add 1 to counter    or if you prefer the Basic approach,
put counter + 1 into counter

are unambiguous. "Add" and "+" are non-overloadable math functions
that take two numeric values and return the numeric sum.

The problem for me is trying to remember to think in Transcript
instead of in C syntax. I'm constantly switching back and forth, as
well as converting routines into Transcript, so I tend to confuse
myself fairly often, forgetting which environment I'm coding in. It
also doesn't help any when I have to do VBA coding and remember Yet
Another Syntax. I think programming in xtalk comes with experience and
familiarity and not trying to force the syntax to be something else.

I *do* find that many functions that I'm converting from some other
language are unnecessary, as they're already present in Transcript, or
they can be coded in much fewer lines and in either case become more
readable code.

ACT> "add 1 to counter" is not clear since it is "human language". Does it
ACT> mean "increment the value of counter by 1" or does it mean "add another
ACT> counter to the counter I already have" (meaning: I have two counters
ACT> now)? Add 1 WHAT to counter - 1 banana or 1 bit? Consider "counter" to

This is just silly. "1" is a defined constant. In your example, would
you consider "counter++" to be adding 1 banana to counter?

ACT> "counter" is a pointer to "12345".
ACT> printf("%s",counter) will output "12345" - just as expected.
ACT> Adding 1 to counter and printf-ing counter again will output "2345" now,
ACT> which is not the same as incrementing the _value_ of counter (type
ACT> casted to int).

You can't get yourself into this kind of trouble in Transcript without
really working at it. Not that it can't be done (don't ask), but there
all sorts of other ways to mess things up in Transcript without having
to worry about dereferencing pointers. And as you point out above,

printf("%s", counter++);

will get you into this same trouble in C.


-- 
-Mark Wieder
 mwieder at ahsoftware.net



More information about the use-livecode mailing list