Achieving High Performance Application in Java Coding! – Part 3

Posted: February 8th, 2010 | Author: TnT Admin | Filed under: Coding | Tags: , , , , | No Comments »

Code with performance in mind!  Follow these next 7 good coding practises that can be easily achieved for high performance!

  • Beware of “Thread.run()” versus the real intend of “Thread.start()” – We may unknowingly code the thread as “run()”.  This will cause the thread to run sequentially instead of concurrently. Beware of this!  Use “Thread.start()” to allow the thread to run concurrently.
  • Use “String.length ()  == 0” instead of “String.equals(“”)” – Both constructs do the same thing of comparing if any characters exists for the string. However, the first construct, “String.length() == 0” is more efficient than the latter one.
  • Avoid calling “String.toCharArray()” – “toCharArray()” method is inefficient as the method reallocates the entire array stored in the string.  However, sometimes this is necessary.  Therefore, as a rule, avoid using “toCharArray()” method if possible.  If not possible, do use it with care.
  • Place “try/catch/finally” block outside loop – Placing “try/catch/finally” blocks inside loops can slow down the execution of code. Do take note that moving the “try/catch/finally” block outside the loop can change program behaviour. An exception that has the “try/catch/finally” block outside the loop will terminate early.  On the other hand, the “try/catch/finally” block inside the loop may cause the program to continue iterating over the loop even after an exception has occurred.
  • Use “StringBuffer.Append ()” instead of “+=” operators for concatenating strings – “Append()” has better improvement over “+=” operators and its recommended to use it instead of “+=” operators for concatenating strings.
  • Do not use “StringBuffer” as constants – Dynamically resizable strings are unnecessary for constant strings (as they do not change).  Instead, use “String” for non-modifiable string constants.
  • Do not open or close JDBC connections in loops – It is inefficient to open and close a connection in loop.  Instead, the connection should be opened before the loop and closed after the loop to avoid redundant calls make to the database.

Related Posts



Leave a Reply