• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which method is better?  RSS feed

 
Stan F Smith
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have 2 methods that do the same thing, I just want to see which way is better, and why? Your input is greatly apreciated.
The difference between the two is one tests for the exception while the other will throw it if nothing is returned. For the record I like the first one I just dont know if it is good practice to thorw an exception like that.






Thanks!
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two pieces of code do different things: the first piece of code returns the status for the first matching list entry, and the second piece of code returns the status for the last matching entry.

And as for the part which you say is different, as far as I can see they both throw an exception if there is no matching list entry. Am I missing something?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you returning a Boolean from a method which has a boolean return type?
And there are paths of execution which return nothing at all. As Paul C has already said, both will throw the Exception otherwise. That looks very peculiar.
 
Stan F Smith
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I should have given more inforamtion.
The stuffList can contain multiple records however it will only have 1 of MY_STUFF or YOUR_STUFF, it cannot have have both. So int the first method as soon as it finds one it retuns it and moves on. In the second method it would still find the one match but once matched it would continue the loop but in the end would return the same result.
The exception is a very rare case but is possible that neither MY_STUFF or YOUR_STUFF exist, in that case I want the program to throw an exception as the user cannot continue.

All that said, which do you think would be the best way to do it? I like the first but I don't know if it is good practice to have the exception like that.

If you ave a different solution I would love to see it as well.

Thanks for all the input.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If neither MY_STUFF or YOUR_STUFF existing is a valid (albeit rare) condition, then you shouldn't use an exception. Better to change the method to return an int where 0 means neither is found, 1 means MY_STUFF was found and 2 means YOUR_STUFF was found (or something like that). This has the advantage that if at some point in the future you want to test for SOMEONE_ELSES_STUFF, it's easy to add a new return value.
You could even create an enum to represent all valid return values and return an instance of that.

Also note that there is no need to use Boolean.TRUE and Boolean.FALSE. Just use the true and false keywords.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:If neither MY_STUFF or YOUR_STUFF existing is a valid (albeit rare) condition, then you shouldn't use an exception. Better to change the method to return an int where 0 means neither is found, 1 means MY_STUFF was found and 2 means YOUR_STUFF was found (or something like that). This has the advantage that if at some point in the future you want to test for SOMEONE_ELSES_STUFF, it's easy to add a new return value.
You could even create an enum to represent all valid return values and return an instance of that.


Or just return the Stuff object which you matched, or null if you didn't match any.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!