Is "getSomething()" a bad method naming pattern?
Methods should tell objects what to do, for example:
circle.paint()
But if I tell an object to getSomething()
, I would tell the object to get "something" (from anywhere) and not to return "something", what is the typical usage of get
methods (getName()
would return "name").
I think that it would be more correct to name the method开发者_如何学C returnSomething()
.
So is get
(as used typically) a bad naming pattern?
The convention probably varies depending on the language you are using, in php (which doesn't support getter/setter methods) the following is quite common:
$myObject=>setSomething($value)
- sets an internal variable of $myObject representing 'something' to $value
$myObject=>getSomething()
- returns an internal variable of $myObject representing 'something'
This is less common in languages like C#, which support getter/setter methods, where you'd probably do the following:
public object Something {
get { return _something; }
set { _something = value; }
}
Then you can use dot syntax to access the private variable:
myObject.Something="something";
string value=myObject.Something;
I personally don't use Get
prefix.
Only prefix I do use for methods that retrieves something is Is
for "indicators".
E.g. payment.IsOverdue()
As for setter methods - those shouldn't exist.
Object state should be defined by itself through invoked behavior.
Get
is not necessary because when we are asking for something, nouns should be used for naming.
Deamon, first of all I think this thing kinda depends on the language.Secondly, I've got this Method Naming Guidelines for you.
The following are examples of correctly named methods.
RemoveAll()
GetCharArray()
Invoke()
I can also say that in the company I am working we always use names like GetSomething()
, GetYourAcceptRateHigher()
for our methods.
精彩评论