HR & Training +91-769-609-5145

Home » Uncategorized » Use constant types for safer and cleaner code

Use constant types for safer and cleaner code

Avoid making typos while encapsulating arbitrary data in legacy systems in an elegant object-oriented way

A couple readers pointed out that one of the constructors for the Smart Int Array has a bug in it. This constructor, which takes only the initial size of the array, calculates the growth size by dividing that initial size by four. If the initial size provided is less than four, the growth size will be calculated as zero, and thus an Array Out Of Bounds Exception will be thrown as soon as the initial size is exceeded. One way to avoid this is to check for a growth size of zero and reset it to one. Another alternative is to define a minimum growth size. I prefer the latter because, the smaller the growth size is, the less efficient the tool becomes.

A few of you also commented that the Vector class has a method called copyInto() which places the vector’s contents into the provided array of type Object. This is of no help for primitive types, which is why I wrote the smart arrays. However, the Smart String Array version is unnecessary, since String is an object, not a primitive, and thus can be passed into the vector’s utility method.

Leave a Reply

Your email address will not be published. Required fields are marked *