麻烦了好名字的功能 [英] Trouble coming up with good names for functions

查看:102
本文介绍了麻烦了好名字的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我经常有一个简单的名称描述一个函数的麻烦。它通常不是为重用而使用的函数中的问题,但通常大的进程需要被分解为子函数。通常这些会得到奇怪的名称,例如 connectionsToAccessLines handleWallVisionSplit 或类似的东西。虽然这些功能只做一件事,为他们提供一个好名字是非常困难的,因为他们只是一个更大的算法的一部分。

So, I often have trouble describing a function in a succinct name. It's usually not a problem in functions that are made for reuse, but often a large process needs to be broken into sub-functions. Often these get strange names, such as connectionsToAccessLines or handleWallVisionSplit or something like that. And while these functions only do one thing, it's very tough to come up with a good name for them because they really are just one part of a larger algorithm.

你在这种情况下吗?这是非常令人沮丧的。

What do you do in this situation? It's very frustrating.

推荐答案

有时如果你不能提出一个好的函数名称, t有一个漂亮,清晰的焦点,需要重构。如果它是一个类方法,也许这个类需要重构。

Sometimes if you can't come up with a good function name it's an indication that the function doesn't have a nice, crisp focus and needs to be refactored. If it's a class method, perhaps the class needs refactoring too.

但是很值得找到最好的名称,因为它使你的代码更容易理解,可用。

But it's well worth the trouble finding the best possible names, since it makes your code so much more understandable and usable.

更新:许多软件工程作者都谈到了命名的重要性。 Henry F. Ledgard的程序谚语(1975)和Brian Kernighan和PJ Plaugher的元素的编程风格(1978)是早期的,仍然值得阅读。 Steve McConnell的精彩代码完成(第2版,2005年)是一个更近的例子,将整个章节投入到主题

Update: Many software engineering authors have talked about the importance of naming. Henry F. Ledgard's Programming Proverbs (1975) and Brian Kernighan and P.J. Plaugher's Elements of Programming Style (1978) were early ones and are still worth reading. Steve McConnell's wonderful Code Complete (2nd Edition, 2005) is a more recent example, devoting an entire chapter to the topic.

编程风格的元素在Strunk和White的 Elements of Style ,其实具有令人惊讶的相关性。他们强调使散文清晰和极其简洁适用于我们的技术写作和评论(和命名),但我一直认为它类似于我们在重构和改进我们的代码时所做的工作。

Elements of Programming Style was in part patterned on Strunk and White's Elements of Style, which actually has a surprising relevance. Their stress on making prose clear and extremely concise applies to our technical writing and comments (and naming), but I've always seen it as analogous to what we do when we refactor and improve our code.

这篇关于麻烦了好名字的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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