How can I reduce code size?

Julien SIMON

Code size is particularly important for post-issuance applets: they are loaded in EEPROM, so the bigger they get, the less application data you can store.

Here are a number of tips that help reduce code size:

  • Keep the number of classes/interfaces to a minimum. Class overhead is over 200 bytes, so fancy OO designs with plenty of patterns and interfaces are not appropriate.
  • Keep the number of methods to a minimum. Increasing member visibility eliminates the need for the usual get/set methods. Not very nice, but efficient.
  • Try to use no more than 3 parameters for virtual methods and 4 for static methods. This way, the compiler will use a number or bytecode shortcuts, which help reduce code size: aload_x (1 byte) instead of aload x (2 bytes), etc.
  • Use the switch-case control structure with care. It generates very verbose bytecode.
  • Reuse local variables instead of declaring new ones.
  • Hunt down unused but initialized local variables.