• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Effective Java: If you could only tell people about one item, what would it be?

 
author & internet detective
Posts: 41071
848
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For me, it would be item 48: Avoid float and double if exact answers are required.

It only took me getting burned once by trying to do monetary calculations with double instead of BigDecimal to learn this one!
 
Marshal
Posts: 5316
324
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without a doubt, item 39: Make defensive copies when needed.

I had written a class that held a list of things, and on that class I'd written a getAllTheThings() type method that returned the whole List. It hadn't occurred to me at the time that all I was doing was dishing out references to that single internal List and as soon as one of those client processed decided to start hoking about with the List chaos ensued. All the other clients that held a reference to the same List started exhibiting unexpected behaviour, it took me hours to figure out what I'd done.
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:Without a doubt, item 39: Make defensive copies when needed.


I had to go back and read this again. It's something I'm not doing and I'm trying to understand when I should be doing it. The last paragraph says "if a class has mutable components that it gets from or returns to it's clients". That would seem to mean just about any parameters or return values, but I'm sure I'm not understanding this correctly and I would end up overusing this technique when it's not needed.

Would you care to take a shot at explaining this in a way that my feeble brain might be able to grok?
 
Jeanne Boyarsky
author & internet detective
Posts: 41071
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Defensive copies is a good one. As an example, suppose you have a cache of forums like we do here. Many places in the code get a reference to that same list. Now suppose one method was written by a new developer who wants to display a view with only five forums in it. So he/she calls remove() a bunch of times on that list object. The problem is that everyone has a reference to the same list object and the cache is now corrupt. A defensive copy would prevent that by making it so everyone doesn't have the same list object.
 
Tim Cooke
Marshal
Posts: 5316
324
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne's example is the exact one that I encountered. I was writing some tests and decided to write a fake DAO backed by a List so I didn't have to get hooked up to Oracle. My DAO just dished out a reference to the same list everywhere so any changes the client of the DAO made to the List was observed by everyone. Mayhem ensued.

In this case it would only fail in my tests as the real DAO constructed new objects each time but it was a harsh reminder of how Java passes variables around.
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if I understand correctly, it really comes down to the question of "is there a possibility of more than one thread accessing (changing) this object". If in doubt, make a defensive copy.

What about static synchronized methods? This code came to mind:


Should I be making a defensive copy of that dateString parameter?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not just multiple threads (although having immutable objects does help with that).

The way I look at it is this: an object should be responsible for maintaining its own state. All changes to that state should go through methods belonging to that object - I have to ask you to change your own state rather than changing it for you.

As soon as an object gives a reference to part of its internal state that is mutable to anyone else, then it has lost control. It no longer has any say in whether that state changes or not. Which means you, as the programmer, can't rely on changes only happening where you expect them to.

In your example, though: Strings are immutable. So a defensive copy there is pointless.

 
I am displeased. You are no longer allowed to read this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic