我应该编写更多描述性的函数名称还是添加注释? [英] should I write more descriptive function names or add comments?

查看:70
本文介绍了我应该编写更多描述性的函数名称还是添加注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个与语言无关的问题,但是我正在徘徊人们在可读性和可维护性方面的偏爱...我的假设情况是我正在编写一个函数,该函数给定一个序列将返回一个包含所有重复元素的副本删除并颠倒顺序.

This is a language agnostic question, but I'm wandering what people prefer in terms of readability and maintainability... My hypothetical situation is that I'm writing a function which given a sequence will return a copy with all duplicate element removed and the order reversed.

/*
*This is an extremely well written function to return a sequence containing 
*all the unique elements of OriginalSequence with their order reversed
*/    
ReturnSequence SequenceFunction(OriginalSequence)
{...}

UniqueAndReversedSequence MakeSequenceUniqueAndReversed(OriginalSequence)
{....}

上面应该是一个清晰的示例,该示例在第一个实例中使用注释,在第二个实例中使用非常冗长的函数名称来描述该函数的动作.

The above is supposed to be a lucid example of using comments in the first instance or using very verbose function names in the second to describe the actions of the function.

干杯

理查德

推荐答案

我更喜欢冗长的函数名称,因为它使调用站点更具可读性.当然,某些函数名称(例如您的示例)可能会变得很长.

I prefer the verbose function name as it make the call-site more readable. Of course, some function names (like your example) can get really long.

也许对于您的示例函数而言,更好的名称是ReverseAndDedupe.哦,现在,我们可以更清楚地知道我们有一个具有两个职责的功能*.也许最好将其拆分为两个功能:ReverseDedupe.

Perhaps a better name for your example function would be ReverseAndDedupe. Uh oh, now it is a little more clear that we have a function with two responsibilities*. Perhaps it would be even better to split this out into two functions: Reverse and Dedupe.

现在,呼叫站点变得更具可读性:

Now the call-site becomes even more readable:

Reverse(Dedupe(someSequence))

* 注意:我的经验法则是,名称中包含"and"的任何功能都有太多职责,需要分解为单独的功能.

*Note: My rule of thumb is that any function that contains "and" in the name has too many responsibilities and needs to be split up in to separate functions.

这篇关于我应该编写更多描述性的函数名称还是添加注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆