函数命名约定

发布于 2024-08-17 02:40:47 字数 1431 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

勿忘初心 2024-08-24 02:40:47

更通用但简单的规则之一是:如果函数更改程序的状态,则函数名称应该是动词;如果函数用于返回某个值,则函数名称应该是名词。

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.

腹黑女流氓 2024-08-24 02:40:47

编写库时要做的一件更重要的事情是每次都使用相同的单词来描述相同的操作。不要在一个类中编写一个名为 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.

花辞树 2024-08-24 02:40:47

这是一个很棒的资源,建议与 Carl 的答案相同:力求流畅使用

根据函数和方法的副作用命名

  • 那些没有副作用的应该读作名词短语,例如x.distance(to: y)i.successor()

  • 那些有副作用的应该读作祈使动词短语,例如,
    print(x)x.sort()x.append(y)

Here is a great resource advising the same as Carl's answer: Strive for Fluent Usage

Name functions and methods according to their side-effects

  • Those without side-effects should read as noun phrases, e.g. x.distance(to: y), i.successor().

  • Those with side-effects should read as imperative verb phrases, e.g.,
    print(x), x.sort(), x.append(y).

情释 2024-08-24 02:40:47

其他前缀?可能是“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).

泪眸﹌ 2024-08-24 02:40:47

如果有一个通用的规则,我想应该是一致的。

还有“on”前缀,在处理事件时广泛使用(即Java Android: onViewCreated )。广泛使用的其他一些前缀或短和/或通用动词(例如 has、get 和 set)包括:

当涉及的逻辑很少(即属性)时,我更喜欢使用名词来表示简单的 getter,但我会使用“get”前缀来表示复杂的操作:

func center() {
   return (a + b) / 2
}

但是,在某些明确使用“get”前缀的语言中,它被广泛扩展(即 Android - Java),常见的做法是使用一些动词,例如“计算”(即 computeVerticalScrollOffset())

此外,在某些语言中(例如 swift)您还可以使用属性设置器,这样您就不必真正使用“set”前缀:

var x: X {
  get {
    return foo(x)
  }
  set {
    x = bar(newValue)
  }
}

// Set x
x = y

最后,还有许多广泛使用的结构,例如instanceofindexOf, ...

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, ...

长发绾君心 2024-08-24 02:40:47

Pro get/set

当一个类有很多方法时,最好使用动词前缀,例如 get/set,来区分各个方法。

PHP 示例:

$foo->setText('Hello world!');
$foo->prependText('So. ');
$foo->appendText(' And welcome');
$x = $foo->getText();

顺便说一句,在匈牙利表示法中,前缀带有小写字母,不会影响关键字。

计数器 get/set

当您只需要两个方法时,在使用参数的上下文中使用相同的名词会更容易。

jQuery 示例:

$('.foo').html();                //get
$('.foo').html('Hello world!'); //set

示例

对于以数组作为参数的函数和静态方法,我使用以下规则:

如果更改仅应在运行时发生:

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

如果更改将永远进行(在数据库或文件中):

deleteFoo(...); // Delete specified properties
insertFoo(...); // Add specified properties
replaceFoo(...); // Add or replace specified properties
updateFoo(...); // Update 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.

jQuery example:

$('.foo').html();                //get
$('.foo').html('Hello world!'); //set

Examples

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

If changes will be made forever (in DB or files):

deleteFoo(...); // Delete specified properties
insertFoo(...); // Add specified properties
replaceFoo(...); // Add or replace specified properties
updateFoo(...); // Update specified properties

For both cases:

$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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文