Qt 的 QList:获取列表中项目数的规范形式是什么?

发布于 2024-09-06 08:19:00 字数 86 浏览 2 评论 0原文

Qt 的 QList 类提供了几种获取列表中项目数量的方法 - 计数、长度和大小。众所周知,一致性很重要,那么这三种方法中哪一种应该是使用的规范/首选方法呢?

Qt's QList class provides several methods for getting the number of items in the list - count, length, and size. As we all know, consistency is important, so which should be the canonical/preferred method to use of those 3?

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

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

发布评论

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

评论(4

喜爱纠缠 2024-09-13 08:19:00

我同意 @Cogwheel 所说的一切,但老实说,我只会选择一个并坚持下去。我认为好的风格会决定,如果“尺寸”对你来说是最好的,那么在任何地方都使用“尺寸”......不要随意地在“计数”、“长度”和“尺寸”之间交替。这将导致潜在的混乱或大量不必要的文档页面访问。

您可以尝试提出其他类型的基本原理,但语言本身甚至不一致。所有STL容器(例如listvector)仅提供“大小”,string类提供“大小”和“长度”等选择

您最喜欢的(或者如果您有多个开发人员,你们应该都同意最喜欢的)并坚持下去。

I agree with everything @Cogwheel said, but in all honesty I would just pick one and stick with it. I think good style would dictate that if "size" sounds the best to you, then use "size" everywhere...don't alternate between "count", "length" and "size" haphazardly. That will lead to potential confusion or a lot of unnecessary trips to documentation pages.

You could try to come up with some other kind of rationale, but the language itself isn't even consistent. All the STL containers (e.g. list, vector) only provide "size", the string class provides "size" and "length", etc.

Pick your favorite (or if you have multiple developers, you should all agree on a favorite) and just stick with it.

请持续率性 2024-09-13 08:19:00

您应该努力实现的一致性是在您的项目内。试图与其他人保持一致并不会真正获得任何好处,除非他们在某些方面是不兼容的。

话虽这么说,函数名称之间存在细微的语义差异(英语,而不是 C++)。如果您可以通过利用差异来使代码更清晰,那么一致性实际上可能对您不利。

The consistency you should work towards is within your project. You're not really going to gain anything by trying to be consistent with everyone else, unless there's some way they'd be incompatible.

That being said, there are subtle semantic differences (in English, not C++) between the names of the functions. If you can make your code clearer by taking advantage of the differences, then consistency may actually work against you.

花开柳相依 2024-09-13 08:19:00

恕我直言,其中任何一个。由于即使不同的开发人员在项目中也遵循不同的函数,因此函数名称是完全自我记录的,从某种意义上说,其他开发人员可以轻松理解该函数的含义。

IMHO, Any one of those. Since even different developers follow different functions within your project, the function names are quite self documented, in the sense that other developers can easily understand what the function is meant to.

别想她 2024-09-13 08:19:00

我通常会选择“尺寸”。最终,它有点随意,但 Qt 容器和 STL 容器通常都有大小,因此很容易在两种类型之间保持一致。也是最短的。每当几个解决方案基本上等效时,我总是选择打字量最少的那个。这是一个简单的经验法则,因此项目中的每个人通常都能记住它。

I usually go with "size." Ultimately, it is a bit arbitrary, but Qt containers and STL containers both generally have a size, so it is easy to stay consistent between the two types. It's also the shortest. Whenever several solutions are basically equivalent, I always go with whichever results in the least amount of typing. It's a simple rule of thumb, so everybody on the project can usually remember it.

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