Extreme Tips for Unmaintainable MIDlets

I stumbled on a blog post containing “Extreme Tips for Lightning-Fast MIDlets“. Let’s look at some of these programming gems:

Avoid synchronization when possible

Mmm, okay. Don’t know about you, but I synchronize code only when I need it. Perhaps the author of this article likes to synchronize on a whim? Not me…

Array-spreading

Example:

// Before
int[][] table; // a 4×4 table

// After
int[] table; // a 1×16 table

WTF?! Let’s look at the pros and cons of this:

Pros:

  • marginally quicker

Cons:

  • extra computation needed to find the correct array element, if the data structure is supposed to model multi-dimensional data. Espcially relevant if the number of the items in the first dimension isn’t fixed at compile-time
  • yields code which is more difficult to maintain. Associated risk of introducing bugs

Loop Unrolling

Yessir, unrolling loops was all the rage in 1980.

Avoiding argument usage

// Bad
return multiply(int a, int b) {
return a * b;
}

// Good
int result;
int a;
int b;

void multiply() {
result = a * b;
}

It gets even better if all the variables and the method are declared “static”.

This is where I start bashing the table with my fists.

It’s a great tip, if you write single-threaded code. I would respectfully suggest that the author needs to read up on threads and thread-safety.

Given his views on avoiding “wasteful” accessor and mutator methods, I’d guess that ‘result’, ‘a’ and ‘b’ are all publically accessible? Violating the encapsulation aim of our OO code? (Oh, hang on… everything’s ’static’ anyway.)

This is just a sample of optimizations that can be easily applied to just about any MIDlet with minimal effort.

It’s only minimal if you don’t have to maintain the code.

While it might be difficult to guage their effectiveness

Dead easy to gauge effectiveness: if another developer kills himself when confronted with your code, I guess you’ve succeeded. Right?

The value in these tips comes mostly from the gained understanding of the bytecode that results from our programming styles and how it is up to us to keep it optimal.

If you feel an urge to look at Java bytecode, then either:

  • you’re stuck firmly in the 1980s, or
  • you’ve chosen the wrong language, or
  • you’ve chosen the wrong career

(or all three)

So what have we gained from all of this? Apparently, the best way to write MIDlets is to code procedurally (ie, without using OO), and to only use a single thread. Grrreat!

Tags: ,
Filed under tech : Comments (0) : Aug 5th, 2008

Leave a Reply