Zero

Programming language questions
Post Reply
User avatar
DGDanforth
Posts: 59
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, CA, U.S.A.
Contact:

Zero

Post by DGDanforth »

I programmed for 14 years in a language called MAINSAIL (Machine Independent SAIL)
SAIL = Stanford AI Language.

One nice feature I miss is the concept Zero. Every datatype had its
value for Zero, for example: REAL 0.0, INTEGER 0, BOOLEAN FALSE, ...

All local variables were guaranteed to be initialized to Zero on entrance to a procedure .
In Component Pascal variables are only initialized to Zero in the outer block of a module.
I don't believe extending that to procedure variables would add much overhead.

Doug
cfbsoftware
Posts: 55
Joined: Wed Sep 18, 2013 10:06 pm
Contact:

Re: Zero

Post by cfbsoftware »

There's currently a discussion on this topic in the ETH Oberon mailing list:

Should one automatically initalize local pointer or procedure variables (safety precaution)?

http://lists.inf.ethz.ch/pipermail/ober ... 13120.html
User avatar
Robert
Posts: 177
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Zero

Post by Robert »

cfbsoftware wrote:There's currently a discussion on this topic in the ETH Oberon mailing list:
Some interest points ... .

In my opinion BlackBox made a good choice: Initialise the POINTERs, do not initialise other variables such as INTEGERs. And it is important that the choice is specified in the Language Report so that careful programmers know they do not need to double assign "myPointer := NIL" at the start of each subroutine.
User avatar
DGDanforth
Posts: 59
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, CA, U.S.A.
Contact:

Re: Zero

Post by DGDanforth »

cfbsoftware wrote:There's currently a discussion on this topic in the ETH Oberon mailing list:

Should one automatically initialize local pointer or procedure variables (safety precaution)?

http://lists.inf.ethz.ch/pipermail/ober ... 13120.html
Interesting. I had not considered procedure variables but they also would be set to Zero.
Why is initializing all local variables considered too expensive?
It would be a simple task to look at old code and count the number of times the
programmer sets a variable to Zero and how often they are set it to some other
non Zero value.

Doug
cfbsoftware
Posts: 55
Joined: Wed Sep 18, 2013 10:06 pm
Contact:

Re: Zero

Post by cfbsoftware »

A few observations:

1. You would also have to count the number of times the programmer set a boolean to FALSE (assuming 0 = FALSE), a SET to {}, a char to 0X or CHR(0) etc. etc.

2. Also, you must only count the first occurrence of that variable being assigned a value in a procedure.

Apart from identifying possible cases:

3. You wouldn't write (I hope)

i := 0;
FOR i := 0 to 10 ...

so why should the compiler generate the extra code?

4. If the intention behind initialising a variable to zero is to try to detect bugs that occur when an unitialised variable is accessed then it is doomed to failure. For example, if the variable should have been assigned an initial value of 1 instead then the bug is less likely to surface than if it had been assigned some random value. e.g. suppose the programmer was supposed to write

k := 1;
a[k] := 42;

But he forgot the k := 1;

The compiler then generates the code:

k := 0; (* secretly *)
a[k] := 42;

This could very well go unnoticed until much later when it is much harder to trace back to the root cause.

If some initialisation was thought to be a good idea then in this case a very large number would be better:

k := 2147483647; (*secretly *)
a[k] := 42;

Assuming range checking was on, the programmer would be immediately alerted when he ran the program that he had made a mistake. However, that still would not help in the case of BOOLEANs etc.

That explains why Pointers and Procedure Variables could be considered to be a special case. If they are initialised to NIL then any attempts to reference them if the programmer has forgotten to assign a sensible value to them will immediately cause a runtime error.
User avatar
Ivan Denisov
Posts: 362
Joined: Tue Sep 17, 2013 12:21 am
Location: Krasnoyarsk, Russia

Re: Zero

Post by Ivan Denisov »

In BlackBox there is useful command Info -> Analyze Module. It will show, if you use some variables without initialization. So if you forget to init some variable to Zero, it will show. The efficiency is much more important than Zero by default.
User avatar
DGDanforth
Posts: 59
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, CA, U.S.A.
Contact:

Re: Zero

Post by DGDanforth »

cfbsoftware wrote:
k := 2147483647; (*secretly *)
a[k] := 42;

Assuming range checking was on, the programmer would be immediately alerted when he ran the program that he had made a mistake.
I like that. IIRC It corresponds to Edsger W. Dijkstra rule of fail as soon as possible.
So I withdraw my desire for the initialization of local variables to Zero.

Doug
Post Reply