What do you think about 'MimeUtility.checkAscii(...)' ?

R

Red Orchid

In JavaMail 1.3.2 Source Code,
there are the three 'checkAscii(...)' methods.
All of them have the variable 'int ascii'.

For details, the following is source of
'checkAscii(byte[] b)':

<quote>
static int checkAscii(byte[] b) {
int ascii = 0, non_ascii = 0;

for (int i=0; i < b.length; i++) {
// ...
if (nonascii(b & 0xff)) // non-ascii
non_ascii++;
else
ascii++; // <-- #1
}
if (non_ascii == 0)
return ALL_ASCII;
if (ascii > non_ascii)
return MOSTLY_ASCII;
return MOSTLY_NONASCII;
}
</quote>

Do you think that the above code is good or not bad
because #1 is trivial and the readability is good or ..?

I think that the above code is bad because #1 is
needless and is not trivial. For example, MIME
message's size can be large if it has attachments.
Java do not have the keywork 'register' of c/c++.

I think that the following is better than the above code.

<code>
static int checkAscii(byte[] b) {
int non_ascii = 0;

for (int i = 0; i < b.length; i++) {

if (nonascii(b & 0xff)) {

non_ascii++;
}
}
if (non_ascii == 0) {

return ALL_ASCII;
}
if ( non_ascii < (b.length >> 1) ) {

return MOSTLY_ASCII;
}
return MOSTLY_NONASCII;
}
</code>

What is your opinion ?
Thanks.
 
C

Chris Uppal

Red said:
In JavaMail 1.3.2 Source Code,
there are the three 'checkAscii(...)' methods.
All of them have the variable 'int ascii'.
if (nonascii(b & 0xff)) // non-ascii
non_ascii++;
else
ascii++; // <-- #1

Do you think that the above code is good or not bad
because #1 is trivial and the readability is good or ..?

I agree that keeping count of the ascii characters as well as the non-ascii is
needless work, and mildly obfucating. I certainly wouldn't have programmed it
that way. But:
if ( non_ascii < (b.length >> 1) ) {
return MOSTLY_ASCII;

Is even more obfuscatory. There can be no possible need for bit-twiddling here
to "gain speed" (even if we accept that this code might be used in a
speed-critcal context[*]) since the comparison is happening outside the inner
loop.

-- chris

([*] which seems unlikely to me -- or rather, it's hard to imagine a context
where this code would be the dominating factor)
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top