#java
5 posts

Simple Java performance tuning tips

Use primitive types rather than wrapper classes wherever possible to minimize overheads as they are stored to the stack and not the heap.
Read more

Simple Java performance tuning tips

  • To start optimizing your app, use a profiler to find the real bottlenecks in the code and then create a performance test suite for the whole application based on that information. Run your tests before and after every attempt at optimization.
  • Use primitive types rather than wrapper classes wherever possible to minimize overheads as they are stored to the stack and not the heap. Avoid BigInteger and BigDecimal as they dramatically slow down calculations and use a lot of memory.
  • If your app uses a lot of replace operations and you aren’t updated to the latest version of Java, consider the Apache Commons StringUtils.replace method rather than String.replace. You can make the change easily by adding a Maven dependency for Apache’s Commons Lang to your app’s pom.xml to replace all instances.
  • Cache especially your more expensive resources or most-used snippets of code, such as database connections or the valueOf method for the Integer class. However, you are creating an overhead and you may need to manage the cache to keep it accessible and remove outdated information, so be sure the tradeoff is worthwhile.

Full post here, 9 mins read

Tips to power-up your Java security

Protect against SQL injections by binding variables in prepared statements, using the prepareStatement() function to validate inputs.
Read more

Tips to power-up your Java security

  • Protect against SQL injections by binding variables in prepared statements, using the prepareStatement() function to validate inputs.
  • Returning mutable objects leaves you vulnerable to unexpected changes in your class state. Instead, use an unmodifiable/immutable collection or a copy of a mutable object to return.
  • Avoid including XSS characters in log messages. Manually sanitize each parameter and configure your logger service to replace such characters.
  • Always validate user input, especially when dealing with files whose location might be specified by user input.
  • Replace predictable random values (java.util.Random) based on clock tickets or other predictable parameters with a secure random class and functions.
  • Eliminate dynamic class loading.

Full post here, 4 mins read

What’s in a name: Java naming conventions

Make methods and functions verbs, implying what they do in 2-3 words in camelCase. Use ‘get’ & ‘set’ to start the names of data fetching and setting functions.
Read more

What’s in a name: Java naming conventions

  • In the base package name, put your company’s domain in reverse order & then add the project name & maybe version - all in lower case.
  • Use nouns, written in CamelCase (with first letter capital), for class names. Class names should say what function or variable to expect from it as well.
  • Choose short, meaningful nouns for variables and fields, saying what values or variables they hold, in camelCase.
  • Avoid single character variables. Avoid underscore & dollar as first letters. For boolean values, start with ‘is’ or ‘has’, since they are yes/no questions.
  • Put constants in all-caps, with underscores to separate words.
  • Make methods and functions verbs, implying what they do in 2-3 words in camelCase. Use ‘get’ & ‘set’ to start the names of data fetching and setting functions.
  • Use similar conventions as classes and interfaces for enums and annotations, respectively, with enums in all-caps.

Full post here, 5 mins read

Lambdas are Not Functional Programming

Rather than spending time trying tricks with Lambdas, you should: Make good use of Generic Types. Declare type parameters & enforce them everywhere. Minimize casting and if instanceOfing.
Read more

Lambdas are Not Functional Programming

Rather than spending time trying tricks with Lambdas, you should:

  • Make good use of Generic Types. Declare type parameters & enforce them everywhere. Minimize casting and if instanceOfing.
  • Make illegal states unrepresentible in code.
  • Make your own data classes immutable and final where possible, use proper immutable collections.
  • Use libraries that avoid runtime magic and reflection where pragmatically possible.

Full post here, 9 mins read

Java debugging at scale: when rare events become commonplace

This story of debugging an intermittent problem with Jira Cloud sheds light on the nature of problem solving in a large and complex software system.
Read more

Java debugging at scale: when rare events become commonplace

Full original post here

This story of debugging an intermittent problem with Jira Cloud sheds light on the nature of problem solving in a large and complex software system.

Salient points from this post:

  • Problem solving can be a very iterative process, asking questions whose answers which lead to new questions is part and parcel of the process.
  • Debugging some things will take iterations of logging in production. With large and complex systems, one can’t always reason about the system’s behavior. Sometimes you will need to add code to determine what is happening in production.
  • Debug the happy path to find the initial symptoms but remember that doing this is just the first step on a long journey.
  • Dead ends are also information. They make you backtrack and re-examine what you know.

Read the entire story to understand the step-by-step process of digging deeper into the problem that led to effective debugging.

9 mins read