Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Red Orchid - 12 Feb 2006 19:24 GMT
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[i] & 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[i] & 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.
Chris Uppal - 13 Feb 2006 12:22 GMT
> In JavaMail 1.3.2 Source Code,
> there are the three 'checkAscii(...)' methods.
> All of them have the variable 'int ascii'.

>         if (nonascii(b[i] & 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)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.