简体   繁体   English

是否值得插入`const`-correctness

[英]Is it worth to insert `const`-correctness

I'm currently confronted with a C++ project written by some senior programmers consisting of about 400 files and 200 classes. 我目前面临的是由一些高级程序员编写的C ++项目,该项目由大约400个文件和200个类组成。

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. 虽然我正在添加一些功能,但对我而言,通常的做法是关注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. 但是,如果我开始声明我的新成员函数const ,那么为了使事情有效,适应旧代码是没有尽头的。

  • Should I invest the amount of time to introduce const -correctness into this code? 我应该投入大量时间将const -correctness引入此代码吗?
  • 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? 这值得么?

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. Const正确性是静态类型的一种附加层,旨在使开发成熟,可靠和健壮的代码变得更加容易。 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. 在这种情况下,从实用的角度来看,在这样的代码库中强制执行const正确性似乎没有显着的附加价值。

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

Should I invest the amount of time to introduce const -correctness into this code? 我应该投入大量时间将const -correctness引入此代码吗?

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. const -correctness是一件好事,所以如果你可以调整代码库来正确使用它,那么这永远不会是坏事。

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 . 这一切都取决于你有多少时间可用以及你可以做的其他事情,更多的是项目管理,更适合程序员.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. 正如molbdnilo正确地指出的那样,这是一个很大的变化,你应该开始之前进行小组讨论。 This would be more appropriate than leaving it to code review in two weeks' time, when you've already done it. 这比在两周后完成代码审查更合适。

It's a difficult issue. 这是一个棘手的问题。 Retrofitting const correctness is a non-trivial job (as you're noticing). 改进const正确性是一项非常重要的工作(正如您所注意到的)。 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. 另一方面,const正确性在某些情况下几乎是必不可少的 - 或者,如果所有编译器都强制执行关于不使用临时初始化对非const的引用的规则。

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. 这是一个项目层面的决定。

yes and yes. 是的,是的。 Const correctness is a Good Thing for a number of reasons, amongst which that it helps prevents bugs. 由于多种原因,const正确性是一件好事,其中有助于防止错误。 I actually already discovered bugs while applying const correctness in a situation similar to yours. 实际上,我已经在类似于你的情况下应用const正确性时发现了错误。

Yes. 是。 Once you get over the hurdle of converting your current code to be const correct it becomes second nature. 一旦你克服了将当前代码转换为const的障碍,它就成了第二天性。

Also if you start to follow a spec like MISRA it requires your code to be const correct (amongst many other things). 此外,如果您开始遵循像MISRA这样的规范,它需要您的代码是正确的(在许多其他事情中)。

There are two technical steps in enforcing const-correctness. 强制执行const正确性有两个技术步骤。

Before you start though, you need to involve your team members, and explain the benefits of const -correctness. 在开始之前,您需要让您的团队成员参与进来,并解释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. 现在,既然我们使用的是StackOverflow,而不是SE,我宁愿专注于技术方法。

The two steps are drawn from the fact that: 这两个步骤来自以下事实:

  • you cannot call a non- const function on a const object 你不能在const对象上调用非const函数
  • you can call a const function on a non-`const object 你可以在非`constst对象上调用const函数

Therefore: 因此:

  1. Tag as const those functions who are 标记为const那些函数
  2. const -ify the variable/parameters/attributes const -ify变量/参数/属性

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. 即使您在团队中遇到轻微的阻力,您仍然可以标记您开发的方法或触摸为const而不会对团队其他成员造成任何阻碍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM