add a single byte to byte()

  • Thread starter Thread starter Guoqi Zheng
  • Start date Start date
David,
I should add:

Another consideration I would consider is how large a buffer are we talking,
if it appeared the buffer was going to from 1 to maybe 20 bytes (based on
the requirements), then I would be less likely not to use the ArrayList.
However if it appeared to be from 100 to 250 bytes+ then I would consider
the ByteBuffer more.

It would depend on how "smelly" the code felt. (Smelly code is a Refactoring
term (http://www.refactoring.com)

Hope this helps
Jay
 
David,
Unfortunately there is no where near enough information (here) to make an
informed decision on it. Hence my statement "may hurt performance" so the OP
could weigh the options on his actual requirements and make an informed
decision. Which via profiling may need to be changed.\

If it was the core function of that class (UUENCODE class)

Obviously there's not enough info here (we have almost no info at all),
but that's why I added to the hypothetical, and specifically asked about
the situation where it *wasn't* the core function. If it's a very
important operation that's used a lot, I think we'd all spend a little
more time on it and get a fairly robust solution. I don't think anybody
disagrees with that, or with the general nature of that solution.

What I was really interested in was what people's approaches would be if
it were just an ordinary work-a-day question, the kind of thing that
pops up a dozen times a day. But hypotheticals of this sort always
leave a lot of ambiguity since there's so many unknown variables
involved, so I admit that's not an easily answered question.
Using a CollectionBase does not feel right here, as the buffer in question
seems like an implementation detail of the UUENCODE functionality, not a
collection of domain objects per se. I normally reserve CollectionBase
classes for collections of domain objects. FWIW a Domain object is the more
general term for business object.

Unless you were suggesting you would inherit from CollectionBase to
implement a ByteBuffer class.

Partially. As I said, I very seldom declare ArrayLists, I reach for a
typed collection almost immediately, and my standard script creates one
derived from CollectionBase. I was trying to describe the situation
where this function looked as if it would be little used, and I just
wanted to get the loop working quickly.
 
David,


Why just to disagree,? I real do not see the argument in this.

This might be the language thing, but "just to disagree" to me implies
I'm being disingenuous or argumentative for argument's sake. If so, I
assure you I'm not. I really do disagree here.
You will use a long, an integer, a double, a variable lenght string, however
a byte array of 400 bytes becomes to much for you?

Long, integers and doubles are individual entities. A string object
maintains its own length. And yes, a byte array of any size where the
"true" length of the array is kept separately is too much for me.

If I absolutely had to do this for performance' sake, I'd immediately
encapsulate it into its own class that did very little except keep track
of the byte length, and I'd test it to death before I felt even a little
comfortable.

It all has to do with how much complexity you're willing to allow in
your programs. Hopefully you can agree that an array that does not
contain Array.Length objects is more complex than a completely filled
array. To me, it's too complex to exist for long inside a function, and
*way* too complex to pass around between functions.
 
David,
I should add:

Another consideration I would consider is how large a buffer are we talking,
if it appeared the buffer was going to from 1 to maybe 20 bytes (based on
the requirements), then I would be less likely not to use the ArrayList.
However if it appeared to be from 100 to 250 bytes+ then I would consider
the ByteBuffer more.

I wouldn't disagree, though it all depends on the centrality of this
operation of course, as you say. This paragraph implies that you would
never use the inline array solution, which is actually the question I
was getting at. What I was really curious about when I posed the
hypothetical was whether the folks in this thread would really reach for
the "fix the array inside the loop" solution which many were suggesting.
 
David,

I putted the "why just disagree" at the wrong text so let that go, it is to
difficult to remake the starting point of that text while the arguing is now
more about an incomplete bytearray.

Your point is that in can confuse. However maybe that is because you are
never using it. That is always with making programs, when you have taken an
approach you know what you are doing, for strangers it can look strange.

This approach will always be with its filledCount right beside the code, the
same as the redim will be beside its code, and the arraylist.count has the
code always used in the code. When it real should be a point you can even
make your own class for it, with your own "populatedcount" property for
that.

Cor
 
David,

I putted the "why just disagree" at the wrong text so let that go,

Okay, as I said I thought it might just be a confusion in my
understanding.
it is to
difficult to remake the starting point of that text while the arguing is now
more about an incomplete bytearray.

Your point is that in can confuse.

Not entirely, and your reduction of my post to that leads to your error
below. My point isn't just that the code can confuse, as you say any
code can be confusing if you've never worked in that field before or if
you aren't used to a specific pattern or style.

My point was that the confusion here stems from additional code
complexity. There isn't anything here that I would assume a decent
programmer isn't familiar with. Arrays, loops, indexes and the ReDim
statement are things I assume every decent programmer can handle easily.
However maybe that is because you are
never using it. That is always with making programs, when you have taken an
approach you know what you are doing, for strangers it can look strange.

Are you suggesting you *often* do this? Familiarity leads me to the
assumption that Array.Length is valid as the count of entries. Are you
suggesting that if I read a lot of your code I'd start to assume
otherwise?
This approach will always be with its filledCount right beside the code,

Which isn't close enough for me, especially when you've got another
property right beside the code that usually refers to the same thing
(i.e., Array.Length). Look, go ahead and disagree, that's fine. As I
said originally, the level of complexity people allow in their code
differs tremendously. But I don't think one can seriously contend
there's not a complexity issue here.


the
same as the redim will be beside its code, and the arraylist.count has the
code always used in the code. When it real should be a point you can even
make your own class for it, with your own "populatedcount" property for
that.

Which is beside the point, since that's precisely the solution you
stated was unnecessary here.
 
David,

Because I have made so many samples for the bytearray in this situation, I
want to turn it around, give me one practical sample where adding a byte to
an arraylist would be a right solution.

So not that you can add a byte to an arraylist, I know I can do that (you
can add any object to a arraylist), however for what situation would it be a
practical solution?

Maybe I can than agree better with you.

Cor
 
David,

Because I have made so many samples for the bytearray in this situation, I
want to turn it around, give me one practical sample where adding a byte to
an arraylist would be a right solution.

So not that you can add a byte to an arraylist, I know I can do that (you
can add any object to a arraylist), however for what situation would it be a
practical solution?

OK, you've got some kind of code reader that sends a stream of bytes to
you, but the verification program (named Foo, naturally) you have only
wants bytes with the upper bit unset.

Private Sub VerifyWithList(ByVal stm As Stream)
Dim list As New ArrayList

Dim input As Integer
Do
input = stm.ReadByte()
If input = -1 Then Exit Do

If input >= 0 AndAlso input <= 127 Then
list.Add(CByte(input))
End If
Loop

Foo(DirectCast(list.ToArray(GetType(Byte)), Byte()))

End Sub

Now, as I mentioned, I tend to use typed collections, so the cast at the
end would go away (which is by far the most complex line), but otherwise
this is probably my first implementation. The two things that leap out
are (a) I'm not thrilled about the way I'm reading the stream here, and
(b) the infinite loop might throw some programmers for a moment, but I
think it's the cleanest implementation (see McConnell's Code Complete
for reams of discussion about formatting this type of loop), but if
someone in code review insisted I be more explicit here I wouldn't
argue.

Here's the function I wouldn't write:

Private Sub Verify(ByVal stm As Stream)
Dim bytes(ESTIMATED) As Byte
Dim index As Integer = 0
Dim input As Integer

Do
input = stm.ReadByte()
If input = -1 Then Exit Do

If input >= 0 AndAlso input <= 127 Then
If index > bytes.Length Then
ReDim Preserve bytes(bytes.Length + ESTIMATED)
End If
bytes(index) = CByte(input)
index += 1
End If
Loop

ReDim Preserve bytes(index - 1)

Foo(bytes)

End Sub

Just to be honest here, I'm posting this as I really did first write it.
And sure enough, it has a bug. The first function is trivial to write
and easy to read. The second function is much trickier. A bug in the
first function would leap out at you at a glance, the bug in the second
function isn't easy to see without looking fairly closely at the code.

And what I absolutely would never do is finish the above like this:

Foo(bytes, index-1)

' while I define Foo as

Public Sub Foo(bytes() as Byte, validBytes as Integer)




One additional point. Jay pretty much had me convinced that I should
worry about boxing much earlier than I tend to. But I can't even
measure the different in timespans that the above two functions take to
run (not with a DateTime object anyway, the noise overwhelms the
difference), even as the array grows to thousands of bytes. That
surprised me, I admit.
 
David,

I know how to do it, my problem for this is that I cannot find a practical
solution for this with an arraylist.

A situation where I would have to add 1 byte to a byte array byte by byte,
while I do not know the length of that bytestring.

The boxing time does not interest me, that is about 1/100000000000
nanosecond

Cor
 
David,

I know how to do it, my problem for this is that I cannot find a practical
solution for this with an arraylist.

A situation where I would have to add 1 byte to a byte array byte by byte,
while I do not know the length of that bytestring.

Okay, this is the third time in a row where you've asked a question,
I've followed up with a fairly specific response that raised specific
issues and asked specific questions, and you've totally ignored my
response and followed up with a non-sequitor. There's really very
little point in continuing the thread.
The boxing time does not interest me, that is about 1/100000000000
nanosecond

This is the kind of thing I mean. I honestly have absolutely no clue
what your point is here. If boxing isn't the issue, I have no idea what
your objection to an arrayList would be, nor do I expect to find out.

Cheers,
 
David,

You just avoiding the answers in my opinion by giving answers on not asked
questions.

I write it again in another way.

Where can I use the arraylist when I have populated that with objects with
single bytes in that.

I have nowhere spoken about boxing, boxing does really not interest me with
an array of 400 bytes.

Cor
 
David,

You just avoiding the answers in my opinion by giving answers on not asked
questions.

I write it again in another way.

Where can I use the arraylist when I have populated that with objects with
single bytes in that.

Go back two posts. Read the post. I have two functions there. Read
them, and the surrounding text. You asked for an example. There's an
example there, as well as two counter-examples.
I have nowhere spoken about boxing, boxing does really not interest me with
an array of 400 bytes.

The issue is how to create that array.
 
David,
OK, you've got some kind of code reader that sends a stream of bytes to
you, but the verification program (named Foo, naturally) you have only
wants bytes with the upper bit unset.

Why would I do that, that is what I am al the time asking?

Creating an not organised array of bytes?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top