Pages

Friday, May 22, 2009

Some Notes on Coding

some of the points you should note while doing development

1. on java

  • Use primitive data types instead of objects as instance variables.
  • Use the final modifier on instance-variable definitions to create immutable internally accessible objects
  • Eliminate unnecessary casts (use jdk1.5 feature)
  • String.equals() is expensive if you are only testing for an empty string. It is quicker to test if the length of the string is 0.
  • When using Vector, ensure that elementAt() is not used inside a loop.
  • Modify java.lang.String to cache the hashCode if you are using many string keys in hash tables [note Sun added this optimization to the String class in SDK 1.3]
  • Reorder CLASSPATH so that the most used libraries occur first
  • Compile java files with the optimizer on.

 

2. On JDBC

  • Turn off autocommit, but don't leave transactions open for too long.
  • Cache any required metadata and use metadata methods as rarely as possible as they are quite slow. Use Connection.setReadOnly(true) to optimize read-only
  • Choose the optimal cursor: forward-only for sequential reads; insensitive for two-way scrolling. Avoid insenstive cursors for queries that only return one row.
  • Use a parametrized remote procedure call (RPC) rather than passing parameters as part of the RPC call, e.g. use Connection.prepareCall("Call getCustName (?)").setLong (1,12345) rather than Connection.prepareCall("Call getCustName (12345)")
  • CachedRowSet provides cached result sets that do not require continuous connection to the database, allowing connections to be reused more efficiently.
  • Use PreparedStatements to batch statements for optimal performance.

 

3. On JSP/Servlet

  • Avoid using the SingleThreadModel interface for servlets: write thread-safe code instead.
  • ServletRequest.getRemoteHost() is very inefficient, and can take seconds to complete the reverse DNS lookup it performs.
  • OutputStream can be faster than PrintWriter. JSPs are only generally slower than servlets when returning binary data, since JSPs always use a PrintWriter, whereas servlets can take advantage of a faster OutputStream.
  • Do not store large object graphs in javax.servlet.http.HttpSession. Servlets may need to serialize and deserialize HttpSession objects for persistent sessions, and making them large produces a large serialization overhead.
  • Call HttpSession.invalidate() to clean up a session when you no longer need to use it.
  • For Web pages that don't require session tracking, save resources by turning off automatic session creation using: <%@ page session="false"%>
  • Timeout sessions more quickly by setting the timeout or using session.setMaxInactiveInterval().
  • Use the include directive <%@ include file="copyleft.html" %>where possible, as this is a compile-time directive (include action is a runtime directive).
  • Use the jspInit() method to cache static data, and release them in the jspDestroy() method.

No comments:

Post a Comment