Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If only one object field is required in a method, what should be passed as a parameter - an object or a field value?

Tags:

java

oop

Lets say there is a method that searches for book authors by book id. What should be passed as a parameter to such method - only book.id (int) or whole book object?

Or another example. In java I need to do some work with current url of the page. What should be passed to such method - only request.getRequestURL() or whole request?

I kind of see benefits from each method but can't come up with good rule when to use what.

Thanks.

like image 347
serg Avatar asked Oct 23 '09 16:10

serg


1 Answers

I am not sure if there is a "rule" to what is best, but I most often pass just the paramaters I need into the method. So in your first example I would only pass in the book.id and in your second example I would only pass in the request.getRequestURL().

I try to avoid passing in more than I need.

like image 166
northpole Avatar answered Oct 31 '22 21:10

northpole