my dog learned polymorphism*
The moose likes Java in General and the fly likes which of the two is good practice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "which of the two is good practice?" Watch "which of the two is good practice?" New topic
Author

which of the two is good practice?

Pavan Kumar
Ranch Hand

Joined: Jan 23, 2004
Posts: 78
This may be a very basic thing, but I want to hear your opinion.

Thanks for your time.

option 1

Option 2


I looked at the byte code, and looks like monitor is released in both the cases. Which one do you prefer to write and why?

Thanks,
Cnu


formerly known as Cnu
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Given that ArrayList itself isn't synchronized, I'm assuming that "listeners" is a synchronized collection like a Vector or Collections.synchronizedList(), or this is really meaningless.

I wouldn't be surprised to find that the bytecode for both is identical. The only difference, then, is clarity. I prefer the first, since it's shorter but no more complex.


[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Ernest Friedman-Hill:
Given that ArrayList itself isn't synchronized, I'm assuming that "listeners" is a synchronized collection like a Vector or Collections.synchronizedList(), or this is really meaningless.

I wouldn't be surprised to find that the bytecode for both is identical. The only difference, then, is clarity. I prefer the first, since it's shorter but no more complex.


Umm...the second looks shorter to me...or maybe I can't count...


Java API Documentation
The Java Tutorial
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
Originally posted by cnu sri:
This may be a very basic thing, but I want to hear your opinion.
..........


So what are the differences in the byte code?

G.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Layne Lund:

Umm...the second looks shorter to me...or maybe I can't count...


Sorry. I should have just said "the shorter one."
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I used javap to check it out. Both jikes and javac do generate slightly different code for the two routines. The shorter one does give rise to slightly shorter bytecode. Basically, for the short one, the return value is loaded onto the stack before the lock is released; for the longer one, it's loaded afterwards. The difference is very slight.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I prefer Option 2, simply because it's easier to read and understand. Option one takes me a couple of seconds more to parse, and why waste any brain cells on that?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I prefer option 1 for the same reason that Ilja prefers option 2.

Also, I think that returning in the middle of a method rather than at the end, particularly if there is more than one return in the method, makes it more difficult to refactor later.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Marilyn de Queiroz:
I prefer option 1 for the same reason that Ilja prefers option 2.


That's interesting - could you please elaborate on what you find easier to parse about option 1?

Here is what happens to me:

Option 1: "OK, there is some synchronized block. After that, iter gets returned. What is iter? Ah, iter gets assigned to in the synchronized block. What's that line above. Duh, it's just the declaration of iter. Let's double check that it's really the same variable. Yes, seems to be. So let's summarize: it returns an iterator for the listeners, wrapped by a new ArrayList."

Option 2: "OK, just a synchronized block with a return in it. Seems to return an iterator of listeners, wrapped by a new ArrayList."

The difference really is just a couple of seconds. Still, I find option 1 to be unnecessarily complex. It's also more likely that it contains a bug - just let's also have a field called itera, and return that one instead...

Also, I think that returning in the middle of a method rather than at the end, particularly if there is more than one return in the method, makes it more difficult to refactor later.


That's true, though I don't see how it's true in this case.

On the other hand I find that early returns can make methods easier to *understand*, which is a prerequisite for refactoring.

Often when I have to refactor a big method, one of the very first things I do is introducing Guard Clauses. After that, it's much easier to understand which steps the method can be separated into. Refactoring the method so that, for example, it's easy to extract methods, isn't too hard, even with the guard clauses present.

Your mileage may vary, of course.
Pavan Kumar
Ranch Hand

Joined: Jan 23, 2004
Posts: 78
Thanks very much for your replies.
I'm assuming that "listeners" is a synchronized collection like a Vector or Collections.synchronizedList()

Yes, indeed it's Collections.synchronizedList().

So what are the differences in the byte code?

I should probably have mentioned that I did not understand most of it,besides asking my question more clearly. but my concern was to find out if the monitor was released in both the cases. I was still not very assured and wanted to hear some more about the issue.

Thanks for your valueable comments and time.

it may not matter much, but here's the complete code,


Cnu,
 
Consider Paul's rocket mass heater.
 
subject: which of the two is good practice?
 
Similar Threads
Need Help for School assignment
Need Help for School assignment
hot spot optimization techniques?
For-eaching an iterator
Collections.synchronizedMap Help