Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Tuesday, April 19, 2016

return null vs. throw exception

Its not wise to return null when any business logic is incorrect its best to throw an exception when there's a validation error or a required field missing.
This way you can propagate the error up the stack

Example:

  if(imageTicket == null) {
   throw new ImageTicketNotFoundException("Could not find a image ticket for the id provided");
  }

Thursday, September 12, 2013

use of isAssignableFrom

Instead of
 @Override
 public boolean equals(Object obj)
 {
  if (this == obj) return true;
  if (obj == null) return false;
  if (getClass() != obj.getClass()) return false;
  final Location other = (Location) obj;
  if (getId() != other.getId()) return false;
  return true;
 }
use
 @Override
 public boolean equals(Object obj)
 {
  if (this == obj) return true;
  if (obj == null) return false;
  if (! getClass().isAssignableFrom( obj.getClass() ) ) return false;
  final Location other = (Location) obj;
  if (getId() != other.getId()) return false;
  return true;
 }
Ref: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#isAssignableFrom(java.lang.Class)

Long vs long

If you are using Long for variable that cannot be null, its best to use the type long instead of Long That way you do not need to check for null Only use wrapper classes when you need to support the concept of undefined

Monday, June 29, 2009

Protected vs. Private methods in Java

Unless you have a specific reason to make the method private - like you expressly want to prevent subclasses from accessing the method - methods should be protected. This way we can subclass this tag.

if/else vs. switch

When writing code that branches on enum values, instead of if/else blocks, use the switch statement like so:

switch (selectedTab) {
case (Tab.SITES):
//do something for sites
case (Tab.CATEGORY):
//do something for category
case (Tab.DEMOGRAPHIC):
//do something for demographics
case (Tab.ALL_SITES):
//do something for all sites
}


Lengthy and nested methods: Methods should small, atomic and easy to read. Break it up like so:

switch (selectedTab) {
case (Tab.SITES):
renderSitesTab();
case (Tab.CATEGORY):
renderCategoryTab();
case (Tab.DEMOGRAPHIC):
renderDemographicsTab();
case (Tab.ALL_SITES):
renderAllSites();
}

Comparison in Enums

Tip on Enums

Instead of using id to do equality comparisons for enums, use the enum itself.

Example: Instead of
if (selectedTab.getId() == Tab.SITES.getId()) {
use:
if (selectedTab.equals(Tab.SITES)) {

StringBuilder vs. StringBuffer

A tip on StringBuilder vs. StringBuffer

  • We should be using StringBuilder over StringBuffer. It’s a drop-in replacement, all the method names and arguments are the same.

  • StringBuilder is not synchronized and therefore doesn’t require object locks everytime a method is called. The only time we’d use StringBuffer is when it’s a member variable with a possibility of multiple threads writing to it.