Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what is the convenience method?

Status
Not open for further replies.

kanghao

IS-IT--Management
Jul 4, 2004
68
KR
what is the convenience method?
"...
When dealing with interfaces of classes, it's best to keep them small. The Law of Demeter says that you should never write code that invokes a getter on an object returned from a getter, for example x.getBlah().getFoo(). Instead, x's class should have a getFoo() method that does that for you. I think that law is wrong. If x.getBlah()returns a complex object, the public interface expands way out of control. Furthermore, changes to the interface of x.getBlah() cause changes to the interface of x. For that reason, I suggest exactly the opposite: Convenience methods with simple alternatives should be removed from the public interface of the class. If you suspect that a public method does not need to be public, you can do an experiment by deprecating it. You won't break the build, and you'll soon find out if anyone cares whether or not it can be called.
... "
-in the article "Make bad code good" at JavaWorld
 
From context alone they are saying that getA().getB() is better. Personally this is a call for the coder to make based on the problem. I would use whatever seems like it would yeild the lest code (which is different in each case).

[plug=shameless]
[/plug]
 
I mean what is the convenience method in general not only in this article.
 
I usually take it for what it says ... it's a method which is not strictly part of a class's main responsibilities but provides some convenient task for the class's clients which would normally have to go via a different route to achieve. This implies that the class providing the convenience method is in a good position to offer the method (for example, it already accesses that functionality from elsewhere and can simply delegate it onwards, etc).

It strikes me that this kind of activity simply increases coupling between classes. The trade-off is that you don't have to go instantiating a class to get hold of one method since it is provided for convenience by an object you already have.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
they're saying that a method
x.getFoo()
would be a more convenient way of saying
x.getBlah().getFoo()

hence, x.getFoo() would be called a "convenience method"

i agree that this is poor practice and increases coupling/clutter etc


-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top