• Like

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Julz Cutaran - Common Pitfalls in Java

  • 993 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
993
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
8
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Common Pitfalls
  • 2. 1. Not specifying columns in your select statement (select *)
  • 3. 2. Select and Transfer . . StringBuffer queryString = new StringBuffer(); queryString.append( &quot;SELECT DISTINCT o.name, o.code, o.price, d.discount “ ); queryString.append( &quot;FROM com.exist.model.Order as o, “ ); queryString.append( &quot;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); } . . .
  • 4. . . . StringBuffer queryString = new StringBuffer(); queryString.append( &quot;SELECT DISTINCT new com.exist.to.OrdersTO &quot; ); queryString.append( &quot;(o.name, o.code, o.price, d.discount) &quot; ); queryString.append( &quot;FROM com.exist.model.Orders as o, &quot; ); queryString.append( &quot;com.exist.model.Discounts as d “ ); . . . Session session = sessionFactory.getCurrentSession(); Query selectQuery = session.createQuery(queryString.toString()); . . . List<OrdersTO> data = selectQuery.list();
  • 5. 3. Not delegating computations . . . double bills = someService.getBills(referenceNo); double payments = someService.getPayments(referenceNo) double due = bills - payments; . . .
  • 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. . . . Computations computations = new Computations(someService, referenceNo); double bills = computations.getBills(); double payments = computations.getPayments(); double due = computations.getDue(); . . .
  • 8. 4. equals String literal variable.equals(“String Literal”);
  • 9. “ String literal”.equals(variable);
  • 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. BigDecimal minuend = new BigDecimal(1.1).setScale(2, RoundingMode.HALF_EVEN); BigDecimal subtrahend = 1.0d; BigDecimal difference = minuend.subtract(subtrahend);
  • 12. 6. using objects in if If (object.getBooleanProperty()) { ... }
  • 13. if (object!=null && object.getBooleanProperty()) { ... } Or if (object!=null && object.getNumericProperty()>0) { ... }
  • 14. Thanks!