One of the more universal, yet simple rules is: Function names should be verbs if the function changes the state of the program, and nouns if they're used to return a certain value.
One more important thing to do when writing a library is to use the same word to describe the same action every time. Don't write a function named getName in one class and another function named retrieveNumber in another class.
Other prefixes? Possibly "isa", though that is only applicable in some situations.
Some languages can communicate "get" and/or "set" with other constructs (specifically, in Common Lisp you can make (setf (get* ...) blah) do the same as what you would've wanted (set* ... blah) do).
If there is a universal rule, I think it should be to be consistent.
There is also the "on" prefix, which is widely used when dealing with events (i.e. Java Android: onViewCreated). Some other prefixes or short and/or generic verbs (such as has, get and set) widely used are:
I prefer using nouns for simple getters when there is very little logic involved (i.e. properties) but I would use the "get" prefix for complex actions:
func center() {
return (a + b) / 2
}
However, in some languages where using explicitly the "get" prefix is widely extended (i.e. Android - Java), the common practice is using some verb such as "compute" (i.e. computeVerticalScrollOffset())
Furthermore, in some languages (e.g. swift) you can also use property setters so you don't really use the "set" prefix:
var x: X {
get {
return foo(x)
}
set {
x = bar(newValue)
}
}
// Set x
x = y
And finally, there are many widely used constructions such as instanceof, indexOf, ...
setFoo($arr); // Replace/delete all properties, i.e. if some elements are not passed, the corresponding properties will get empty values.
setFoo([]); // Delete all properties
setFoo(); // Set all properties by default
delFoo($arr); // Delete specified properties
addFoo($arr); // Add/replace specified properties
$arr = getFoo(); // Get all properties
$val = getFoo($level1, $level2, ...); // You can obtain the value of the given level, placing the list of arguments
or
$val=getFoo()[$level1][$level2];
Pro get/set
When a class has many methods, it is better to use verb prefixes, such as get/set, to distinguish methods from each other.
PHP example:
$foo->setText('Hello world!');
$foo->prependText('So. ');
$foo->appendText(' And welcome');
$x = $foo->getText();
By the way, in Hungarian notation prefixes go with a small letter and will not detract from keyword.
Counter get/set
When you need only two methods, it is easier to use the same noun in the context of using parameters.
For functions and static methods with arrays as parameters I use the following rule:
If changes should occur only at run time:
setFoo($arr); // Replace/delete all properties, i.e. if some elements are not passed, the corresponding properties will get empty values.
setFoo([]); // Delete all properties
setFoo(); // Set all properties by default
delFoo($arr); // Delete specified properties
addFoo($arr); // Add/replace specified properties
$arr = getFoo(); // Get all properties
$val = getFoo($level1, $level2, ...); // You can obtain the value of the given level, placing the list of arguments
or
$val=getFoo()[$level1][$level2];
发布评论
评论(7)
更通用但简单的规则之一是:如果函数更改程序的状态,则函数名称应该是动词;如果函数用于返回某个值,则函数名称应该是名词。
One of the more universal, yet simple rules is: Function names should be verbs if the function changes the state of the program, and nouns if they're used to return a certain value.
编写库时要做的一件更重要的事情是每次都使用相同的单词来描述相同的操作。不要在一个类中编写一个名为 getName 的函数,而在另一个类中编写另一个名为 retrieveNumber 的函数。
One more important thing to do when writing a library is to use the same word to describe the same action every time. Don't write a function named getName in one class and another function named retrieveNumber in another class.
查看
项目
Have a look at
Projects
这是一个很棒的资源,建议与 Carl 的答案相同:力求流畅使用
Here is a great resource advising the same as Carl's answer: Strive for Fluent Usage
其他前缀?可能是“isa”,尽管这只适用于某些情况。
某些语言可以与其他结构交流“get”和/或“set”(具体来说,在 Common Lisp 中,您可以使 (setf (get* ...) blah) 执行与您想要的 (set* . ..等等)做)。
Other prefixes? Possibly "isa", though that is only applicable in some situations.
Some languages can communicate "get" and/or "set" with other constructs (specifically, in Common Lisp you can make (setf (get* ...) blah) do the same as what you would've wanted (set* ... blah) do).
如果有一个通用的规则,我想应该是一致的。
还有“on”前缀,在处理事件时广泛使用(即Java Android:
onViewCreated
)。广泛使用的其他一些前缀或短和/或通用动词(例如 has、get 和 set)包括:当涉及的逻辑很少(即属性)时,我更喜欢使用名词来表示简单的 getter,但我会使用“get”前缀来表示复杂的操作:
但是,在某些明确使用“get”前缀的语言中,它被广泛扩展(即 Android - Java),常见的做法是使用一些动词,例如“计算”(即
computeVerticalScrollOffset()
)此外,在某些语言中(例如 swift)您还可以使用属性设置器,这样您就不必真正使用“set”前缀:
最后,还有许多广泛使用的结构,例如
instanceof
,indexOf
, ...If there is a universal rule, I think it should be to be consistent.
There is also the "on" prefix, which is widely used when dealing with events (i.e. Java Android:
onViewCreated
). Some other prefixes or short and/or generic verbs (such as has, get and set) widely used are:I prefer using nouns for simple getters when there is very little logic involved (i.e. properties) but I would use the "get" prefix for complex actions:
However, in some languages where using explicitly the "get" prefix is widely extended (i.e. Android - Java), the common practice is using some verb such as "compute" (i.e.
computeVerticalScrollOffset()
)Furthermore, in some languages (e.g. swift) you can also use property setters so you don't really use the "set" prefix:
And finally, there are many widely used constructions such as
instanceof
,indexOf
, ...Pro get/set
当一个类有很多方法时,最好使用动词前缀,例如 get/set,来区分各个方法。
PHP 示例:
顺便说一句,在匈牙利表示法中,前缀带有小写字母,不会影响关键字。
计数器 get/set
当您只需要两个方法时,在使用参数的上下文中使用相同的名词会更容易。
jQuery 示例:
示例
对于以数组作为参数的函数和静态方法,我使用以下规则:
如果更改仅应在运行时发生:
如果更改将永远进行(在数据库或文件中):
对于这两种情况:
Pro get/set
When a class has many methods, it is better to use verb prefixes, such as get/set, to distinguish methods from each other.
PHP example:
By the way, in Hungarian notation prefixes go with a small letter and will not detract from keyword.
Counter get/set
When you need only two methods, it is easier to use the same noun in the context of using parameters.
jQuery example:
Examples
For functions and static methods with arrays as parameters I use the following rule:
If changes should occur only at run time:
If changes will be made forever (in DB or files):
For both cases: