The following is the code for a method of an object that maintains a list of types (IType[] types) in an array as well as a field that stores the number of non-null types in the array (int typeCount). What the method should do is to resolve all types in the list (resolve either returns the type it was called at or another IType). I am now wondering which of the following implementations is better:
for (int i = 0; i < this.typeCount; i++)
{
this.types[i] = this.types[i].resolve(markers, context);
// vs
IType t1 = this.types[i];
IType t2 = t1.resolve(markers, context);
if (t1 != t2)
{
this.types[i] = t2;
}
}
Note that this pattern occurs in many places throughout the project, of which many can be considered boilerplate.
null. It means you should check fornullbefore callingresolve. And thei < this.typeCountdoesn't smell good. I would prefer ai < this.types.length. The code above _may_work if all nulls are effectively grouped at the end of the array.i < this.typeCountis true,this.types[i]will be true. ThetypeCountandtypesfields are basically inlined from theArrayListimplementation.typeCountexists so I can initially createtypesasnew IType[3]without actually stating that it contains 3 types. There is also anaddTypemethod in the class that increasestypeCountand 'resizes' the array at wish.