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

Discussion in 'Java' started by Red Orchid, Feb 12, 2006.

  1. Red Orchid

    Red Orchid Guest

    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.
     
    Red Orchid, Feb 12, 2006
    #1
    1. Advertising

  2. Red Orchid

    Chris Uppal Guest

    Red Orchid wrote:

    > 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)
     
    Chris Uppal, Feb 13, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bredal Jensen
    Replies:
    5
    Views:
    482
    bredal Jensen
    Jul 7, 2004
  2. =?Utf-8?B?Sm9l?=
    Replies:
    2
    Views:
    514
    David Jessee
    Jan 20, 2005
  3. Replies:
    0
    Views:
    643
  4. Replies:
    1
    Views:
    911
    Jack Klein
    Apr 13, 2005
  5. David Mark
    Replies:
    17
    Views:
    265
    David Mark
    Mar 23, 2010
Loading...

Share This Page