Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Julz Cutaran - Common Pitfalls in Java

1,267 views

Published on

Published in: Technology, Economy & Finance
  • Be the first to comment

  • Be the first to like this

Julz Cutaran - Common Pitfalls in Java

  1. 1. Common Pitfalls
  2. 2. 1. Not specifying columns in your select statement (select *)
  3. 3. . . StringBuffer queryString = new StringBuffer(); queryString.append("SELECT DISTINCT o.name, o.code, o.price, d.discount “); queryString.append("FROM com.exist.model.Order as o, “); queryString.append("com.exist.model.Discounts as d “); . . . Session session = sessionFactory.getCurrentSession(); Query selectQuery = session.createQuery(queryString.toString()); List<Order> orders= selectQuery.list(); . . . List<OrderTo> orderTo = new ArrayList<OrderTo>(); ... for(Order order:orders){ OrderTo oto = new OrderTo oto.setName(order.getName()); . . . orderTo.add(oto); } . . . 2. Select and Transfer
  4. 4. . . . StringBuffer queryString = new StringBuffer(); queryString.append("SELECT DISTINCT new com.exist.to.OrdersTO "); queryString.append("(o.name, o.code, o.price, d.discount) "); queryString.append("FROM com.exist.model.Orders as o, "); queryString.append("com.exist.model.Discounts as d “); . . . Session session = sessionFactory.getCurrentSession(); Query selectQuery = session.createQuery(queryString.toString()); . . . List<OrdersTO> data = selectQuery.list();
  5. 5. 3. Not delegating computations . . . double bills = someService.getBills(referenceNo); double payments = someService.getPayments(referenceNo) double due = bills - payments; . . .
  6. 6. public class Computations { private SomeService someService; private double bills; private double payments; private double due; public Computations(SomeService someService, String reference) { bills = someService.getBills(referenceNo); payments = someService.getPayments(referenceNo) due = bills - payments; } ... <getters of bills, payments and due> }
  7. 7. . . . Computations computations = new Computations(someService, referenceNo); double bills = computations.getBills(); double payments = computations.getPayments(); double due = computations.getDue(); . . .
  8. 8. 4. equals String literal variable.equals(“String Literal”);
  9. 9. “String literal”.equals(variable);
  10. 10. 5. Computing using double or Double double minuend = 1.1d; double subtrahend = 1.0d; double difference = minuend – subtrahend; Or Double minuend = 1.1d; Double subtrahend = 1.0d; Double difference = minuend.doubleValue() – subtrahend.doubleValue();
  11. 11. BigDecimal minuend = new BigDecimal(1.1).setScale(2, RoundingMode.HALF_EVEN); BigDecimal subtrahend = 1.0d; BigDecimal difference = minuend.subtract(subtrahend);
  12. 12. 6. using objects in if If (object.getBooleanProperty()) { ... }
  13. 13. if (object!=null && object.getBooleanProperty()) { ... } Or if (object!=null && object.getNumericProperty()>0) { ... }
  14. 14. Thanks!

×