VBCorLib for VB6

Wednesday, March 22, 2006

Byte Arrays to MemoryStream

Ok, Byte arrays have been used in VB for a very long time. My coworkers use them the same way over and over again. They want to keep appending more bytes to the array, so they manually resize the array as more capacity is needed. Atleast they do that much, instead of allocating some massive amount initially and only use 10% of it.

I see this kind of stuff all over the place. I see it repeated in code in everyone's projects. I see it in questions people post asking how to extend the array as they need to... Everyone gives the same answer... Redim Preserve.

I have yet to see anyone suggest creating a dynamic byte list.... you know, something that will increase capacity as needed, then let you retrieve the final result. Something that can be reused time and time again without having to ever worry about handling a dynamic byte array.... Something like a MemoryStream!

From what I can tell, most people are more concerned with execution speed than a good design, code reuse, and maintainability.

I've asked my coworkers where they think my code or their code is slow. They have no answer. They feel that if they can't see all the code in front of them all at once, then it must be running 1000 times slower. They have no confidence in the hidden code, no trust of what they have written before. It seems the older the code is, the less it is trusted. I wrote the MemoryStream class at the beginning of the VBCorLib project and I still trust it. I wonder how many others don't trust their own code.

I use the MemoryStream all the time. It saves me lots of time and I trust it. Yes I wrote it, but atleast I know to trust my own code. I use byte arrays in times when they are required, such as with data transfer on serial ports or sockets. But, unless I'm doing something CPU intensive, I will choose the MemoryStream to make things simple.

- Kelly