是否值得插入“const”-正确性

发布于 2024-12-06 21:02:44 字数 319 浏览 3 评论 0原文

我目前面临一个由一些高级程序员编写的 C++ 项目,其中包含大约 400 个文件和 200 个类。

代码写得很好,运行良好且稳定。

当我添加一些功能时,对我来说,关心 const 正确性只是普通做法。

但是,如果我开始声明我的新成员函数 const,那么为了使事情正常工作而调整旧代码就没有尽头。

  • 我是否应该投入大量时间来引入 const-正确性 进入这段代码?
  • 更糟糕的是,我必须触摸和更改旧的成熟代码并解释 向前辈们汇报一下我在代码审查期间所做的事情。值得吗?

I'm currently confronted with a C++ project written by some senior programmers consisting of about 400 files and 200 classes.

The code is well elaborated, works fine and stable.

While I'm adding some features, for me it is just ordinary practice to take care about const-correctness.

But if I start to declare my new member functions const, there is no end with adapting old code in order make things work.

  • Should I invest the amount of time to introduce const-correctness
    into this code?
  • Even worse, I have to touch and alter the old mature code and explain
    to the seniors what I did during the code review. Is it worth it?

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

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

发布评论

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

评论(7

天生の放荡 2024-12-13 21:02:44

常量正确性是静态类型之上的一种附加层,旨在使开发成熟、可靠和健壮的代码变得更加容易。你说你已经有了后者。在这种情况下,从实用的角度来看,在此类代码库中强制执行 const 正确性似乎没有重大的附加价值。

Const correctness is a kind of additional layer over static typing that is meant to make it easier to develop mature, reliable and robust code. You say that you already have the latter. In that case, enforcing const correctness in such codebase doesn't seem to have significant, added value from the pragmatic point of view.

骑趴 2024-12-13 21:02:44

我应该投入大量时间在这段代码中引入 const-正确性吗?

如果您觉得自己可以在合理的时间内完成所有工作,当然可以。 const - 正确性是一件好事,因此如果您可以调整代码库以正确使用它,那么这永远不会是坏事。

这一切都取决于您有多少可用时间以及您还可以做什么,这更多的是关于项目管理,并且更适合 programmers.SE< /a>.

更糟糕的是,我还要去触碰、修改旧的成熟代码,并向前辈解释我在代码审查期间做了什么。值得吗?

对于他们(以及其他所有人)来说,这当然是值得的。听起来他们会在代码审查中学到很多东西,这太棒了!


编辑

正如 molbdnilo 正确指出的那样,这是一个很大的变化,在开始之前您绝对应该对此进行小组讨论。这比您已经完成后在两周内进行代码审查更合适。

Should I invest the amount of time to introduce const-correctness into this code?

If you feel that you can get it all done in a reasonable time, sure. const-correctness is a good thing, so if you can adjust the codebase to employ it properly then that can never be bad.

It all comes down to how much time you have available and what else you could be doing instead, which is more about project management and more appropriate on programmers.SE.

Even worse, I have to touch and alter the old mature code and explain to the seniors what I did during the code review. Is it worth it?

It's certainly worth it for them (and, by extension, to everybody else). It sounds like they will learn a lot in code review, which is fantastic!


Edit

As molbdnilo rightly points out, this is a big change and you should definitely have a group discussion about it before starting. This would be more appropriate than leaving it to code review in two weeks' time, when you've already done it.

初相遇 2024-12-13 21:02:44

这是值得的……除非你有更重要的事情要做。

It's worth the effort... Unless you have more important things to do.

老街孤人 2024-12-13 21:02:44

这是一个很难的问题。改造 const 正确性并非易事
工作(正如你所注意到的)。如果代码在其他方面是干净的并且
可维护,可能不应该轻易进行。另一方面
另一方面,const 的正确性在某些情况下几乎是至关重要的——或者会
是,如果所有编译器都强制执行不初始化 a 的规则
对带有临时值的非常量的引用。

如果您不是代码的唯一所有者,则要做的就是
与其他相关人员讨论该问题,共同做出决定
它是否重要,如果重要的话,请安排完成它所需的时间
被认为是重要的。你不应该做的只是开始介绍它
你自己,作为你被要求做的改变的“副作用”。
这是项目级别的决策。

It's a difficult issue. Retrofitting const correctness is a non-trivial
job (as you're noticing). If the code is otherwise clean and
maintainable, it probably shouldn't be undertaken lightly. On the other
hand, const correctness is almost essential in some cases—or would
be, if all compilers enforced the rule about not initializing a
reference to non-const with a temporary.

If you're not the sole owner of the code, the thing to do would be to
discuss the issue with the other people involved, decide in common
whether it is important, and program the time necessary to do it if it
is deemed important. What you shouldn't do is just start introducing it
on your own, as a "side effect" of the changes you were mandated to do.
It's a project level decision.

强者自强 2024-12-13 21:02:44

是的,是的。常量正确性是一件好事,原因有很多,其中之一是它有助于防止错误。实际上,我在与您类似的情况下应用 const 正确性时已经发现了错误。

yes and yes. Const correctness is a Good Thing for a number of reasons, amongst which that it helps prevents bugs. I actually already discovered bugs while applying const correctness in a situation similar to yours.

江挽川 2024-12-13 21:02:44

是的。一旦克服了将当前代码转换为 const 正确的障碍,它就会成为第二天性。

此外,如果您开始遵循 MISRA 这样的规范,它要求您的代码保持正确(除许多其他事项外)。

Yes. Once you get over the hurdle of converting your current code to be const correct it becomes second nature.

Also if you start to follow a spec like MISRA it requires your code to be const correct (amongst many other things).

颜漓半夏 2024-12-13 21:02:44

强制常量正确性有两个技术步骤。

但在开始之前,您需要让团队成员参与进来,并解释 const 正确性的好处。如果你不能说服你的队友,那么就不值得开始这个……

现在,由于我们在 StackOverflow,而不是 SE,我宁愿专注于技术方法。

这两个步骤源于以下事实:

  • 您不能在 const 对象上调用非 const 函数,
  • 但可以在其上调用 const 函数非 const 对象

因此:

  1. 将那些 const 的函数标记为
  2. const - 化变量/参数/属性

第一步是非侵入性的,并且已经产生了自己的结果好处,因为它可以防止意外修改标记方法中类的内部属性。

即使您在团队中面临轻微阻力,您仍然可以将您开发或接触的方法标记为 const,而不会妨碍团队的其他成员。

There are two technical steps in enforcing const-correctness.

Before you start though, you need to involve your team members, and explain the benefits of const-correctness. If you cannot convince your team mates, it's not really worth launching into this...

Now, since we're on StackOverflow, and not SE, I'd rather concentrate on the technical approach.

The two steps are drawn from the fact that:

  • you cannot call a non-const function on a const object
  • you can call a const function on a non-`const object

Therefore:

  1. Tag as const those functions who are
  2. const-ify the variable/parameters/attributes

The first step is non-invasive and already yields its own benefits, since it will prevent accidental modifications of the inner attributes of a class within the tagged method.

Even if you face slight resistance in your team, you can still tag the methods you develop or touch as const without any hindrance for the rest of the team.

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