Object Oriented Programming

Bug rates in a Smalltalk project

September 9, 2004 15:52:10.155

There has recently been considerable discussion on comp.lang.smalltalk and comp.object on the merits of static versus dynamic type checking. The static type advocates claim that static type checking eliminates an entire category of errors while the dynamic typing advocates claim that those errors rarely occur.

I'm working on a VisualAge/Smalltalk and GemStone/S project that has been in production for several years. In that time, the system has captured all the unhandled exceptions along with tracebacks for us to analyze and find bugs. These logs have been captured for over three years.

I cannot release the results of the analysis of these logs (for reasons of security), but I categorized 95% of them and found only one single report could have been detected by static typing. It was a "mustBeBoolean" exception because a method returned the receiver instead of a boolean. The problem was fixed immediately after the log was recorded and it never recurred.

Interestingly, some of the errors would have been masked by static typing because they involved performing arithmetic on uninitialized variables. In most static typing languages, integer variables are initialized to 0 which isn't always the right default. For us, they initialized to nil and raised an exception when we tried to use them in a calculation instead of quietly giving us the wrong answer.

Comments

capture unhandled exceptions

[Alan Wostenberg] September 9, 2004 22:42:59.610

"the system has captured all the unhandled exceptions along with tracebacks for us to analyze and find bugs" -- this is a very cool idea, David. I think I'll implement it here at Northern Natural Gas.

null types

[Isaac Gouy] September 10, 2004 2:28:42.054

David: "masked by static typing ... arithmetic on uninitialized variables"
Masked by default variable initialization, not by static typing.

void main(String[] args){     
   int x;
   x++;
}
I:\Nice\test\Test.nice: line 3, column 4:
Variable x is not initialized

David: "I categorized 95% of them ... only one ... could have been detected by static typing"
What about the "initialized to nil and raised an exception" ?

void main(String[] args){     
   let x = null + 5;
}
I:\Nice\test\Test.nice: line 2, column 17:
No possible call for +.

Thank you for at least trying to add some data amongst the opinions.

Re: Bug rates in a Smalltalk project

[ James Robertson] September 10, 2004 3:34:36.292

Comment on Bug rates in a Smalltalk project by James Robertson

More hand waving Isaac. Note that in Smalltalk we get null initialization without the developer having to remember to do it.

Re: Bug rates in a Smalltalk project

[ David Buck] September 10, 2004 6:43:20.618

Comment on Bug rates in a Smalltalk project by David Buck

Isaac: If the variable is declared as an instance variable, can your compiler detect that it's uninitialized? I doubt it. It would have to be a runtime test at the very least and this gives the same effect as in Smalltalk (raising an exception). Detecting this at compile time is only possible in trivial cases.

The likely case is that unitialized variables aren't detected at compile time or ar run time. In that case, the variable as to contain some sort of data or it won't be valid. This data would be used in calculations and give answers which may be wrong. The right answer is to raise a runtime exception which is what Smalltalk does.

C# and uninitialized variables

[David Buck] September 10, 2004 9:11:56.532

Some people have commented that languages like C# detect uninitialized variables at compile time. This is true for temporary variables but not always true for instance variables. Check out the following C# code:

class InitializationTestClass
{
	int x;

	static void Main(string[] args)
	{
		InitializationTestClass test = new InitializationTestClass();
		System.Console.WriteLine (test.Calculate().ToString());
	}

	public int X
	{
		get {return x;}
		set {x = value;}
	}

	public int Calculate()
	{
	return x+5;
	}
}

This code compiles cleanly and executes printing "5" but does so using an uninitialized variable x. The compiler can't detect it and neither can the runtime environment. The error, therefore, is being masked.

detect that it's uninitialized? I doubt it

[Isaac Gouy] September 10, 2004 11:17:13.919

David: "can your compiler detect that it's uninitialized? I doubt it"
In this language "If no default value is given, then every call to the constructor must specify the value for this field."

class MyVal { int x; }

void main(String[] args){     
   let y = new MyVal();
}

I:\Nice\Test\Test.nice: line 5, column 12:
Fields of class Test.MyVal require initial values.
Use the following syntax:
  new Test.MyVal(x: value)

Class Test.MyVal has the following fields:
  int x

Extract from C# spec

[Charles Cook] September 10, 2004 11:24:58.295

Second attempt at including extract from C# spec:

The following categories of variables are automatically initialized to their default values:

  • Static variables.
  • Instance variables of class instances.
  • Array elements.
The default value of a variable depends on the type of the variable and is determined as follows:

  • For a variable of a value-type, the default value is the same as the value computed by the value-types default constructor (11.1.1).
  • For a variable of a reference-type, the default value is null.
Note: Initialization to default values is typically done by having the memory manager or garbage collector initialize memory to all-bits-zero before it is allocated for use. For this reason, it is convenient to use all-bits-zero to represent the null reference. end note

Were your doubts answered?

[Isaac Gouy] September 15, 2004 11:54:54.546

David: "can your compiler detect that it's uninitialized? I doubt it"
Were your doubts answered?

Hardly

[Sean Malloy] September 15, 2004 17:59:12.374

Just look at the comments by Charles Cook.

Class instance variables are auto initialized to default values. Theres a slew of bugs waiting to happen right there.

No, the doubts weren't answered.

Charles Cook was talking about C#

[Isaac Gouy] September 15, 2004 20:45:04.605

Sean: "auto initialized to default values"
Charles Cook was writing about C#.
In the example I gave, instance variables were not auto-initialized.