The Nightstar Zoo

Nightstar IRC Network - irc.nightstar.net
It is currently Mon Nov 20, 2017 8:00 pm

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sat Dec 31, 2005 3:17 pm 
Offline
Nightstar Graveyard Daemon
User avatar

Joined: Mon Jun 03, 2002 8:30 pm
Posts: 1071
Location: Wouldn't you rather observe my Velocity?
Here is a piece of code that basically traverses an object list. It's unit test code, and it replaces this expression:

Code:
PackageReservationDb testPackageReservation = DbManager.getPerson(5).getReservations().first().getPackageReservations().get(0);


With this code:

Code:
PackageReservationDb testPackageReservation = getPrFromFirstReservation( DbManager.getPerson(5) );


This is necessary for two reasons:
  1. On a purely theoretical level, it violates the Law of Demeter.
  2. On a purely practical level, that expression is repeated in about 30 places and keeps throwing NullPointerException and NoSuchElementException, and I have no idea where or why.
Here is the replacement method:
Code:
   private PackageReservationDb getPrFromFirstReservation(PersonDb person, int prIndex) {
      PackageReservationDb prdb = null;
      assertNotNull(person);
      
      SortedSet<ReservationDb> reservations = person.getReservations();
      assertTrue( reservations.size() > 0);
      
      ReservationDb reservation = reservations.first();
      assertNotNull(reservation);
      
      List<PackageReservationDb> prdbs = reservation.getPackageReservations();
      assertTrue( prdbs.size() > prIndex );
      
      prdb = prdbs.get(prIndex);
      assertNotNull(prdb);
      
      return prdb;
   }


So, the catch is, there's another expression in the code I need to replace:
Code:
PackageReservationDb testPackageReservation = DbManager.getPerson(10).getReservations().last().getPackageReservations().get(0);


I don't want to write a whole 'nother method that differs only in 3 characters:
Code:
ReservationDb reservation = reservations.[b]fir[/b]st();
ReservationDb reservation = reservations.[b]la[/b]st();


I suppose I could pass in a boolean firstOrLast, and that's probably what I'll do. But even that level of duplication seems crappy to me. I can't help but wonder if there's a cleaner "more Java" way to do this. Not that I like Java, but hey, when in Rome's sewers, poop like a Roman.

Any thoughts?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 01, 2006 2:54 am 
Offline
Energizer Bunny
User avatar

Joined: Wed May 22, 2002 12:24 am
Posts: 1634
I mentioned this on IRC, but I figure posting it here would work too. You could subclass SortedSet to have a firstOrLast method.

Vorn


Top
 Profile  
 
PostPosted: Tue Jan 03, 2006 4:07 pm 
Chalain wrote:
On a purely theoretical level, it violates the Law of Demeter.

<voice quality="bad" apologies="Geoffrey Rush">And third, the Law of Demeter is more of a set of what you'd call 'guidelines' than actual rules. Welcome aboard the Black Pearl, Mister Brady.</voice>

Chalain wrote:
I don't want to write a whole 'nother method that differs only in 3 characters

Actually, I suspect that's exactly the proper solution ('proper', as always, being subjective). Write the second method with a one-word change. Now, put the code for those two copy&pasted methods next to eachother, and then step back and shade your eyes from the blindingly obvious refactor that needs to be done next.

That's right, now you have three methods One that does the work, and two that do nothing but call into that one.

Oh, and sure, your business method now needs to be passed in a boolean firstOrLast, but the only code that has to knwo about that parameter is in the other two (very, very short) methods you just wrote. All of the various places in your code that calls it merely have to think about whether they're calling getPrFromFirstReservation or getPrFromLastReservation.

Of course, you asked for a "more Java" way of doing things, which I can't help you with. My solutions tend to be a lot more general and a lot less subtle than the "elegant" ones that rely on quirks of a particular language. ;)


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group