If only one object field is required in a method,

2020-06-17 05:37发布

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.

标签: java oop
12条回答
【Aperson】
2楼-- · 2020-06-17 06:06

(Note: this is a dissenting view regarding the accepted answer.)

Well, there is an implicit rule set in context of domain modeling. If the receiver is performing tasks independent of the domain model then you pass the field. Otherwise, you should pass the object and the model specific action is made explicit by the act of the receiver accessing the id property of the 'Book' object. Most importantly, if accessing the property ever evolves beyond simply returning the reference of a field (e.g. certain actions in the property accessor) then clearly you do NOT want to chase all instances in your code where you dereferenced the property before passing it into various methods.

Further considerations are the consequences (if any) of accessing the field before the call cite, or, inside the receiver.

查看更多
萌系小妹纸
3楼-- · 2020-06-17 06:06

If you're writing a DAO of sorts, you should consider having a BookSelector which can be built up like: new BookSelector().byId(id).bySomethingElse(somethingElse) and pass this selector instead of having a proliferation of findByXYZ methods.

查看更多
smile是对你的礼貌
4楼-- · 2020-06-17 06:14

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.

查看更多
叼着烟拽天下
5楼-- · 2020-06-17 06:14

Think about maintaining the code in the long run. Any method you expose is a method you'll have to support for your users going forward. If bookId is all that's needed for the forseeable future, then I'd go with just passing in that: that way, anyone who has a bookId can use your method, and it becomes more powerful.

But if there's a good chance that you may need to refactor the lookup to use some other attributes of Book, then pass in Book.

查看更多
The star\"
6楼-- · 2020-06-17 06:16

There's no rule actually, you should be straightforward with the info you need, in that case the book.id. If you consider extending / sharing your search in the future, the you can have an overloaded method to accept a book object so that you can search by other attributes of the book object.

查看更多
smile是对你的礼貌
7楼-- · 2020-06-17 06:16

I agree with the previous posters. I wanted to add that if you find yourself needing multiple properties of the object (id, title, author) then I'd suggest passing the object (or an interface to the object). Short parameter lists are generally preferable.

查看更多
登录 后发表回答