Skip to main content

Badly written code in J2ME?

4 replies [Last post]
captainfreedom
Offline
Joined: 2007-01-10
Points: 0

Just like to mention that I was looking at the source code of J2ME and I see many cases of badly written code. For instance look at:
phoneME_feature\cldc\src\javaapi\cldc1.1\java\io\DataOutputStream.java

public final void writeChar(int v) throws IOException {
write((v >>> 8) & 0xFF);
write((v >>> 0) & 0xFF);
}

v >>> 0 does nothing, and basically wastes a CPU cycle for no reason.
Now, normally if you did that I would say it's of no importance, but when you are writting an API it's different (for obvious reasons)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Pal Szasz

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Well, the RI (reference implementation) isn't optimized at all. It's
main goal is to be easy to understand.

Btw, a C/C++ compiler would optimize null shift, so actually it's not
CPU wasting. I'm guessing newer java compilers do the same as well.
And actually I prefer it that way as well, it's a bit clearer to read
and understand.

meinterest@MOBILEANDEMBEDDED.ORG wrote:
> Just like to mention that I was looking at the source code of J2ME and I see many cases of badly written code. For instance look at:
> phoneME_feature\cldc\src\javaapi\cldc1.1\java\io\DataOutputStream.java
>
> public final void writeChar(int v) throws IOException {
> write((v >>> 8) & 0xFF);
> write((v >>> 0) & 0xFF);
> }
>
> v >>> 0 does nothing, and basically wastes a CPU cycle for no reason.
> Now, normally if you did that I would say it's of no importance, but when you are writting an API it's different (for obvious reasons)
> [Message sent by forum member 'captainfreedom' (captainfreedom)]
>
> http://forums.java.net/jive/thread.jspa?messageID=192593
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff KVM-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".
>

- --
Szasz Pal
- ----------------
Space Software Studio
http://www.spacesoftwarestudio.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFpVWiCclUI0EjozQRAq2pAKC3dNsHhya0nl8tfxGWkC41mLEWlgCgso4m
UTTR6c+GZ9MSKQn0iJied/U=
=jk06
-----END PGP SIGNATURE-----

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

captainfreedom
Offline
Joined: 2007-01-10
Points: 0

Is that code not part of the Java implementation on the phone?
If so you can't assume that it will be optimised

danila
Offline
Joined: 2006-09-25
Points: 0

I agree the code was written this way for better readability.
I suspect javac doesn't optimize away zero shift.
This affects static footprint, as the bytecode for the
method is 2 bytes larger than it could be.
Yet, it doesn't affect performance, as zero shift will be
eliminated by the JIT-compiler of the phoneME Feature VM.

Danila

captainfreedom
Offline
Joined: 2007-01-10
Points: 0

The java specification doesn't require shift operator optimization, therefore you can't assume it's implemented.
Considering how badly some KVMs are written I wouldn't be surprised if it isn't.
Also, it's illogical to shift by zero bits, and therefore the code is more confusing. Really it's just badly written code.
That was just an example though, there is a lot more stuff like that in there if you look around.