Common Pitfalls Experienced in Java

919 views

Published on

A presentation by Julz Cutaran, Exist Software Engineer, for the meetup of the Association of Filipino Java Developers or PinoyJUG at the TechBar in Ortigas on 03 Nov. 2010.

Published in: Technology, Economy & Finance
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
919
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Common Pitfalls Experienced 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!

×